Reputation: 35
I have a list from split() that look like this:
row[0] = ['one', 'two', 'three']
row[1] = ['three', 'four', 'five']
I write it to a text file and it looks like this:
['one', 'two', 'three']
['three', 'four', 'five']
And now I want to make a list again from my text file that would look like this:
row[0] = ['one', 'two', 'three']
row[1] = ['three', 'four', 'five']
How am I supposed to do that?
Upvotes: 0
Views: 263
Reputation: 21
If you are just trying to serialize a list to disk for later use by a python program, I would suggest pickleing the list. Why reinvent the wheel when Python has serialization built in?
import pickle
with open('outfile', 'wb') as fp:
pickle.dump(row, fp)
To read it back:
with open ('outfile', 'rb') as fp:
row = pickle.load(fp)
Upvotes: 2
Reputation: 329
text = "['one', 'two', 'three']"
text = text[2:-2]
text_list = text.split("', '")
Hacky but gets the job done.
Also please reconsider why you want the text file to look like
['one', 'two', 'three']
Upvotes: 1