按行读取txt,并生成字典
def read_file(filename): #读取数据输入字典
f = open(filename)
count = -1
dictionary_all = {}
while True:
line = f.readline()
line = line.strip() #去掉每行头尾空白
if line:
count+=1
l = line.split(' ') #文件中数据间的分隔符
dictionary_all[count]=l
else:
break
f.close()
return dictionary_all
if __name__ == '__main__':
resultpath = r'新建文本文档.txt'
a = read_file(resultpath)
print("a = ",a ,type(a))
运行结果:
运行结果