Python Forum

Full Version: Writing list as a file, then reading that file as a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a python script that generates date from a simulation. (Python version 2.7)
I am trying to write that simulated data to text files, then open these files in another python script. What I need in the end is a 1000x200 matrix of floats. I have the following code for writing the files.

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


In each of my 1000 rounds, my ~200 length list gets written to gainstext

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

Then, I import it in another script as:

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))
As a list, I get only a list of length 1. As a string it is a string of length 2million and something. It shows up as a long list of floats saperated by commas, here is a sampl:
Output:
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]']
I want each number between the commas to be a separate entry. How can I do this?

I am using the following python packages:

networkx
matplotlib.pyplot
numpy
scipy stats
random
pandas as pd
seaborn
(Oct-16-2019, 08:52 PM)Zoastria_Balnala Wrote: [ -> ]I want each number between the commas to be a separate entry.

contents.split(',') should split it by the comma. Not sure if there is a faster way since its a long string.

>>> s = '1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677, 1.7130853299339677'
>>> s.split(',')
['1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677', ' 1.7130853299339677']
you can save as 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'>
Thanks for your help!