python - 列出为文件,然后将该文件作为python中的列表读取

标签 python

我有一个从模拟生成日期的python脚本。 (Python版本2.7)
我试图将模拟数据写入文本文件,然后在另一个python脚本中打开这些文件。最后,我需要的是一个1000x200的浮动矩阵。我有以下代码用于编写文件

tradereturn = agent.trade(agentset,t,G,seller,buyer,connections,tradeprobability,buyer_contpayoff[t],seller_contpayoff[t],buyer_offer[t],seller_offer[t]) 
print(tradereturn)

gains.append(tradereturn[0]) #gains is a list that should have about 200 entries
trade.append(tradereturn[1]) #trade is a list    


在我的每1000回合中,我的〜200个长度清单被写入到增益文本中

for listitem in gains:
    gainstext.write('%s\n' % listitem)


然后,将其导入另一个脚本中,如下所示:

gains = open("gains.txt", "r")

if gains.mode == "r":
    contents = gains.readlines()
    print(contents)
    print(type(contents))
    print(len(contents))

    contents = str(contents)
    print(len(contents))


作为列表,我只得到长度为1的列表。作为字符串,它是长度为200万的字符串。它显示为一串用逗号分隔的浮点数,这是一个示例:

输出:


  1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677, 1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,1.7130853299339677,0.6261130766021059,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596,0.7265157347076596, 0.7265157347076596、0.7265157347076596、1.5338201626994945]']


我希望逗号之间的每个数字都是单独的条目。我怎样才能做到这一点?

我正在使用以下python软件包:


网络
matplotlib.pyplot
麻木
科学统计
随机
熊猫作为pd
海生的

最佳答案

contents.split(',')应该用逗号分隔。由于字符串较长,因此不确定是否有更快的方法。

>>> s = '1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677'
>>> s.split(',')
['1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677']


你也可以另存为JSON

>>> import json
>>> zz = [1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 0.6261130766021059, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 1.5338201626994945]
>>> with open('zz.json', 'w') as fp:
...     json.dump(zz, fp)
... 
>>> with open('zz.json') as fp:
...     xx = json.load(fp)
... 
>>> len(xx)
78
>>> xx
[1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 0.6261130766021059, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 0.7265157347076596, 1.5338201626994945]
>>> type(xx)
<class 'list'>

关于python - 列出为文件,然后将该文件作为python中的列表读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487862/

相关文章:

python - 如何在 Python 中创建一个 csv 文件,并将其导出(放入)到某个本地目录

python - 是否有 Eclipse 插件可以构建用于分发的 python 可执行文件?

python - 使我的 NumPy 数组跨进程共享

python - 使用本地 pip 安装导出 conda 环境

python - 为什么 os.symlink 使用相对于目标的路径?

python - 如何通过使用 pandas 施加限制来处理异常值?

python 正则表达式 : html

python - 为什么对全局变量的错误分配会提早引发异常?

python - pyOpenSSL 创建一个 pem 文件

python - 混淆矩阵取值错误