python 讀取文本文檔中的數據


 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM