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