python read readline readlines 差別

read() : 讀取整個文件
程式範例:
with open(‘test.txt’) as f:
test_data = f.read()
print( test_data )

會讀取整個文件

readline() : 讀取下一行
程式範例:
with open(‘test.txt’) as f:
test_data = f.readline()
print( test_data )

讀取一行

readlines() : 讀取整個文件,遍歷
程式範例:
with open(‘test.txt’) as f:
test_data = f.readlines()
print( test_data )

所以字串變成 list

本篇發表於 python。將永久鍊結加入書籤。

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>