Data Science/python

텍스트 파일 한 줄 씩 읽기 readline(), readlines()

jay3108 2021. 12. 24. 17:36

1. readline() : 개행 문자를 기준으로 한 줄 씩 읽기

f= open('dream.txt','r')
f.readline()
'I have a dream a song to sing\n'
f.readline()
'to help me cope with anything\n'
f.readline(-1)
'if you see the wonder of a fairy tale\n'

 

2. readlines() : 개행 문자를 기준으로 전체을 읽고 한 줄씩 리스트로 반환하기

f.readlines()
['you can take the future even\n',
 'if you fail I believe in angels\n',
 'something good in everything']