84669 Lernen von Personen
152542 Lernen von Personen
20005 Lernen von Personen
5487 Lernen von Personen
7821 Lernen von Personen
359900 Lernen von Personen
3350 Lernen von Personen
180660 Lernen von Personen
48569 Lernen von Personen
18603 Lernen von Personen
40936 Lernen von Personen
1549 Lernen von Personen
1183 Lernen von Personen
32909 Lernen von Personen
欢迎选择我的课程,让我们一起见证您的进步~~
两种方法:
with open("file") as fh: for line in fh: print(line.strip())
with open("file") as fh: line = fh.readline() while line: print(line.strip()) line = fh.readline()
最简洁优雅又高效的自然是第一种, 如果题主非要用readline(), 则可以使用第二种, while循环, 读到最后一行没有内容会退出循环, 中间有空行不要紧, 空行不等于结尾(\n != EOF)
readline()
while
\n != EOF
http://www.jb51.net/article/5...
for ... in ...:
with open('filename') as fp: for line in fp: # do something
with open('filename','r') as f:
while True: line=f.readline() if line: print (line) else: break
你說的沒錯, readlines 是絕對不要使用的!
readlines
請參考 Python 如何實現並行查找關鍵字所在的行 和 Never call readlines() on a file
一般的文件讀取(從頭到尾讀一遍), 請使用 for line in file 來迭代文件, 簡潔又不容易出錯
for line in file
readline 可能會用在當讀取順序比較複雜的時候
readline
我回答過的問題: Python-QA
1. for ... in ...
2. while fh.readline():
http://www.jb51.net/article/5...
for ... in ...:
with open('filename','r') as f:
你說的沒錯,
readlines
是絕對不要使用的!請參考 Python 如何實現並行查找關鍵字所在的行 和 Never call readlines() on a file
一般的文件讀取(從頭到尾讀一遍), 請使用
for line in file
來迭代文件, 簡潔又不容易出錯readline
可能會用在當讀取順序比較複雜的時候我回答過的問題: Python-QA