It's a very basic thing, but I tried to find out how to read a text file that is python line by line.
with open('./test.txt') as f:
for line in f:
print line
https://journal.lampetty.net/entry/wp/418
Probably the above is the smartest. I've seen a number of samples that use read (), readline (), readlines (), etc., but I don't think I need to use them if I'm processing line by line.
read()use
with open('./test.txt') as f:
lines = f.read()
for l in lines.split("\n"):
print(l)
readline()use
with open('./test.txt') as f:
line = f.readline()
while line:
print(line.rstrip("\n"))
line = f.readline()
readlines()use
with open('./test.txt') as f:
lines = f.readlines()
for l in lines:
print(l.rstrip("\n"))
Python has too many solutions to what you want to do, so it's not good to spend a lot of time thinking about which one is better. ..
Recommended Posts