[英]python read from text file line by line
此腳本將文件加載到dataset
列表中:
dataset = []
with open(filename, 'r') as f_in:
for items in f_in:
dataset.append(items.split())
print(dataset)
印刷:
[['a', 'b', 'c'], ['b', 'c'], ['a', 'b', 'c'], ['d'], ['b', 'c']]
您可以逐行閱讀,然后按單詞拆分每一行:
dataset = []
with open(filename, 'r') as fp:
for line in fp.lines():
dataset.append(line.split())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.