python 读取文本文档中的数据

时间:2023-03-09 03:11:04
python 读取文本文档中的数据
 import os
dir = input('Please input the file dir:')#提示输入文件路径
while not os.path.exists(dir):#判断文件是否存在
dir = input('Cann\'t find the file,Please input the correct file dir:')
data = open(dir,'r')#打开文件
buf = []#缓存文件中数据的变量
for lines in data:
value = lines.split('\t')#读出每行
buf.append(list(map(int,value)))#转换为数字list并缓存
print(value)
data.close()
print(buf)

测试txt内容如下

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