파이썬(Python) 텍스트 파일 라인별 읽는 방법
2월 28, 2023
In Python |
파이썬(Python) 텍스트 파일 라인별 읽는 방법
읽을 파일이 다음과 같은 경우:
data.txt
a
b
c
d
readlines()
path = "data.txt"
with open(path) as f:
lines = f.readlines()
print(lines)
결과:
['a\n', 'b\n', 'c\n', 'd']
splitlines()
path = "data.txt"
with open(path) as f:
lines = f.read().splitlines()
print(lines)
결과:
['a', 'b', 'c', 'd']