1. 程式人生 > >python 讀取文本文檔中的數據

python 讀取文本文檔中的數據

nes () 緩存 txt 文件 數字 print line input

 1 import os
 2 dir = input(Please input the file dir:)#提示輸入文件路徑
 3 while not os.path.exists(dir):#判斷文件是否存在
 4     dir = input(Cann\‘t find the file,Please input the correct file dir:)
 5 data = open(dir,r)#打開文件
 6 buf = []#緩存文件中數據的變量
 7 for lines in data:
 8     value = lines.split(\t)#讀出每行
9 buf.append(list(map(int,value)))#轉換為數字list並緩存 10 print(value) 11 data.close() 12 print(buf)

測試txt內容如下

123 222 345 123
345 111 222 333
123 111 222 333

python 讀取文本文檔中的數據