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.
I am using the following python packages:
networkx
matplotlib.pyplot
numpy
scipy stats
random
pandas as pd
seaborn
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