Python Forum
Writing list as a file, then reading that file as a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing list as a file, then reading that file as a list
#1
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
Reply
#2
(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']
Recommended Tutorials:
Reply
#3
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'>
Reply
#4
Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How can I change the uuid name of a file to his original file? MaddoxMB 2 874 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020