Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write and read a list from file

This is a slightly weird request but I am looking for a way to write a list to file and then read it back some other time.

I have no way to remake the lists so that they are correctly formed/formatted as the example below shows.

My lists have data like the following:

test data here this is one group :)  test data here this is another group :) 
like image 419
Ryflex Avatar asked Jun 20 '13 23:06

Ryflex


People also ask

How do I read a list from a file in Python?

To read a file into a list in Python, use the file. read() function to return the entire content of the file as a string and then use the str. split() function to split a text file into a list. To read a file in Python, use the file.

How do you read a file in Python and store it in a list?

You can read a text file using the open() and readlines() methods. To read a text file into a list, use the split() method.


2 Answers

If you don't need it to be human-readable/editable, the easiest solution is to just use pickle.

To write:

with open(the_filename, 'wb') as f:     pickle.dump(my_list, f) 

To read:

with open(the_filename, 'rb') as f:     my_list = pickle.load(f) 

If you do need them to be human-readable, we need more information.

If my_list is guaranteed to be a list of strings with no embedded newlines, just write them one per line:

with open(the_filename, 'w') as f:     for s in my_list:         f.write(s + '\n')  with open(the_filename, 'r') as f:     my_list = [line.rstrip('\n') for line in f] 

If they're Unicode strings rather than byte strings, you'll want to encode them. (Or, worse, if they're byte strings, but not necessarily in the same encoding as your system default.)

If they might have newlines, or non-printable characters, etc., you can use escaping or quoting. Python has a variety of different kinds of escaping built into the stdlib.

Let's use unicode-escape here to solve both of the above problems at once:

with open(the_filename, 'w') as f:     for s in my_list:         f.write((s + u'\n').encode('unicode-escape'))  with open(the_filename, 'r') as f:     my_list = [line.decode('unicode-escape').rstrip(u'\n') for line in f] 

You can also use the 3.x-style solution in 2.x, with either the codecs module or the io module:*

import io  with io.open(the_filename, 'w', encoding='unicode-escape') as f:     f.writelines(line + u'\n' for line in my_list)  with open(the_filename, 'r') as f:     my_list = [line.rstrip(u'\n') for line in f] 

* TOOWTDI, so which is the one obvious way? It depends… For the short version: if you need to work with Python versions before 2.6, use codecs; if not, use io.

like image 199
abarnert Avatar answered Sep 22 '22 17:09

abarnert


As long as your file has consistent formatting (i.e. line-breaks), this is easy with just basic file IO and string operations:

with open('my_file.txt', 'rU') as in_file:     data = in_file.read().split('\n') 

That will store your data file as a list of items, one per line. To then put it into a file, you would do the opposite:

with open('new_file.txt', 'w') as out_file:     out_file.write('\n'.join(data)) # This will create a string with all of the items in data separated by new-line characters 

Hopefully that fits what you're looking for.

like image 39
Derek Peterson Avatar answered Sep 22 '22 17:09

Derek Peterson