码迷,mamicode.com
首页 > 编程语言 > 详细

python 读取文本文档中的数据

时间:2018-10-17 00:01:19      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:col   correct   int   import   class   文件   提示   The   路径   

 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 读取文本文档中的数据

标签:col   correct   int   import   class   文件   提示   The   路径   

原文地址:https://www.cnblogs.com/dlutccj/p/9801060.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!