Pythonでテキストファイルを読む方法まとめ
1.readline()
file = open(“startnews24python.txt")
while 1:
line = file.readline()
if not line:
break
pass # do something
file.close()
2.fileinput
import fileinput
for line in fileinput.input(“startnews24python.txt"):
pass
3.readlines()
file = open(“startnews24python.txt")
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something
file.close()
4.イテレータ
file = open(“startnews24python.txt")
for line in file:
pass # do something
file.close()