如何读取Python中的文本文件编号的数组 [英] how to read array of numbers from text file in python

查看:278
本文介绍了如何读取Python中的文本文件编号的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code为:

 阵列= [[1.630217208498539],[0.019929319226538452]
FO =打开(file.txt的,W)
fo.write(STR(阵列))
fo.close()

这将保存.txt文件的阵列,在.txt文件内容是二维数组:

[1.630217208498539],[0.019929319226538452]]

和我想这个数组,因为它是返回到另一个程序,这样我可以使用这个数组用于进一步计算(读阵列不应该是一个字符串)


解决方案

 进口泡菜 numlist = [[1.630217208498539],[0.019929319226538452] OUTFILE =打开(log.txt的,WB) 和pickle.dump(numlist,OUTFILE) outfile.close() INFILE =打开(log.txt的,RB) 和pickle.load(INFILE)
 [1.630217208498539],[0.019929319226538452]

I have written code as:

array = [[1.630217208498539], [0.019929319226538452]] 
fo = open("file.txt", "w")
fo.write(str(array))
fo.close()

That will save the array in .txt file, the content in the .txt file is in 2d array as:

[[1.630217208498539], [0.019929319226538452]]

And I want this array as it is back to the another program so that i can use this array for further calculation (the read array should not be a string)

解决方案

 import pickle

 numlist = [[1.630217208498539], [0.019929319226538452]] 

 outfile = open("log.txt", "wb")

 pickle.dump(numlist, outfile)

 outfile.close()

 infile = open("log.txt", "rb")

 pickle.load(infile)
 [[1.630217208498539], [0.019929319226538452]] 

这篇关于如何读取Python中的文本文件编号的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆