웹 크롤링 뜯어먹기 #3 | 자주 쓰는 Python 문법 모음 | with open(), Dataframe, try-except

두더지·2025년 6월 2일
post-thumbnail

< 이전 글


1. with open() 읽기, 쓰기, 추가하기

📁 ex_3.py | 실행 전 프로젝트 구조

.
├── ex_1.py
├── ex_2.py
├── ex_3.py
└── txt_examples

📄 ex_3.py | 파일 생성 예제 코드

with open('txt_examples/ex_3.txt', 'w') as f:
    f.write('This is a test file.\n')
    f.write('It contains some text.\n')
    f.write('This is the third line.\n')

📁 ex_3.py | 실행 후 프로젝트 구조

.
├── ex_1.py
├── ex_2.py
├── ex_3.py
└── txt_examples
    └── ex_3.txt
  • txt_examples/ex_3.txt 경로에 ex_3.txt 파일이 생성됨을 확인할 수 있다.
  • ※ 주의: 디렉토리는 자동 생성되지 않으므로, 사전에 수동으로 생성해야 한다.

✏️ with open('파일경로', mode) 기본 문법

mode는 파일을 어떤 용도로 열 것인지 지정하는 옵션이다.

모드 (mode)의미파일이 없을 때파일이 있을 때
'r'읽기 (read)에러 발생내용 읽기 가능
'w'쓰기 (write)새로 생성내용 덮어씀
'a'추가 (append)새로 생성맨 뒤에 덧붙임

✏️ 파일 입출력 예제 코드

# 1. 파일 쓰기 예제
# - 기존 내용을 덮어쓰고 새 내용 저장
with open('txt_examples/ex_3.txt', 'w') as f:
    f.write('This is a test file.\n')
    f.write('It contains some text.\n')
    f.write('This is the third line.\n')

# 2. 파일 읽기 예제
# - 파일 전체 내용을 읽어 출력
with open('txt_examples/ex_3.txt', 'r') as f:
    content = f.read()
    print("File content:")
    print(content)

# 3. 파일 추가 예제
# - 기존 파일 끝에 새 줄을 추가
with open('txt_examples/ex_3.txt', 'a') as f:
    f.write('This line is added at the end of the file.\n')

# 추가 후 다시 읽어 확인
with open('txt_examples/ex_3.txt', 'r') as f:
    content = f.read()
    print("File content (with line added):")
    print(content)

💬 출력 예시

File content:
This is a test file.
It contains some text.
This is the third line.

File content (with line added):
This is a test file.
It contains some text.
This is the third line.
This line is added at the end of the file.

2. Pandas | Dataframe

📁 ex_4.py | 실행 전 프로젝트 구조

.
├── csv_examples
├── ex_1.py
├── ex_2.py
├── ex_3.py
├── ex_4.py
└── txt_examples
    └── ex_3.txt

📄 ex_4.py | Dataframe 예제 코드

import pandas as pd

states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
population = [39538223, 29145505, 21538187, 20201249, 12812508]

dict_states = {'State': states, 'Population': population}
df_states = pd.DataFrame.from_dict(dict_states)
df_csv = df_states.to_csv('csv_examples/states_population.csv', index=False)

print(df_states)

📁 ex_4.py | 실행 후 프로젝트 구조

.
├── csv_examples
│   └── states_population.csv
├── ex_1.py
├── ex_2.py
├── ex_3.py
├── ex_4.py
└── txt_examples
    └── ex_3.txt

3. try-except 구문

📄 ex_5.py | 에러발생 예제 코드

#Handling exceptions errors: Try-Except
#예를 들어, 
#List에 여러 개의 타입 values가 섞여 있을 때
#TypeError가 발생할 수 있음.

example_list = [2,4,6, "california", 8, 10]

for element in example_list:
    print(element/2)

ㄴ 💬 출력예시 | TypeError 발생

1.0
2.0
3.0
Traceback (most recent call last):
  File "/Users/choiwoojin/Developer/web-crawling-tutorial/ex_5.py", line 9, in <module>
    print(element/2)
          ~~~~~~~^~
TypeError: unsupported operand type(s) for /: 'str' and 'int'

📄 ex_5_exception.py | try-except 적용 예제 코드

#Handling exceptions errors: Try-Except
#예를 들어, 
#List에 여러 개의 타입 values가 섞여 있을 때
#TypeError가 발생할 수 있음.

example_list = [2,4,6, "california", 8, 10]

# try-except 구문을 사용하여 예외 처리
#try:
    # 예외가 발생할 수 있는 코드
#except 예외타입:
    # 예외가 발생했을 때 실행할 코드

for element in example_list:
    try: 
        print(element/2)
    except:
        print(f"Error: {element} is not a number, cannot divide by 2.")

ㄴ 💬 출력예시

2.0
4.0
6.0
Error: california is not a number, cannot divide by 2.
8.0
10.0

참고

profile
일단 하긴 합니다.

0개의 댓글