Write and read a list from file

125,881

Solution 1

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.

Solution 2

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.

Solution 3

Let's define a list first:

lst=[1,2,3]

You can directly write your list to a file:

f=open("filename.txt","w")
f.write(str(lst))
f.close()

To read your list from text file first you read the file and store in a variable:

f=open("filename.txt","r")
lst=f.read()
f.close()

The type of variable lst is of course string. You can convert this string into array using eval function.

lst=eval(lst)
Share:
125,881
Ryflex
Author by

Ryflex

I'm unsure what to put here :/

Updated on July 09, 2022

Comments

  • Ryflex
    Ryflex almost 2 years

    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 :)
    
  • Ryflex
    Ryflex almost 11 years
    I like the pickle library, that's pretty sweet and works perfectly. I've already implemented it and it seems to be working okay. Thank you, I'll mark you as a green tick as soon as it lets me.
  • rluks
    rluks over 9 years
    unicode-escape... I had to open it as 'wb' because of TypeError: must be str, not bytes
  • abarnert
    abarnert over 9 years
    @Pan.student: You're using Python 3.x, right? In Python 2, which is what this question was asking about, str and bytes are the same type, and the difference between binary files and text files is just newline translation. In Python 3, str and unicode are the same type, and the difference between binary files and text files is that text files automatically encode and decode for you. (You can get 3.x-like behavior if 2.x if you're careful, including 3.x-style text files with io.open, but the asker wasn't doing that.)
  • abarnert
    abarnert over 9 years
    @Pan.student: In 3.x, you're probably better off with using open(the_filename, 'w', encoding='unicode-escape') and letting the file object take care of encoding it for you. (You can do the same in 2.x with either io.open or the codecs module, but I think explicit encoding makes it easier to see what's going on in 2.x, where the danger of mixing bytes and text is much higher.)
  • Hugo
    Hugo over 9 years
    With the second option on Python 2.7, instead of multiple lines, it saves a single line with plaintext \n in between. Changing f.write((s + u'\n').encode('unicode-escape')) to f.write(s.encode('unicode-escape') + u'\n') fixes it.
  • abarnert
    abarnert over 9 years
    @hugo: That's exactly what I meant by doing it manually. If you're writing pure-2.x code (as opposed to 2.x/3.x code) I think it's often clearer to call encode than to use the io module. Or, putting it another way, if you really want the io module, there's a good chance you want to write 3.x code with 2.x backward compat instead of 2.x code.
  • brokenfoot
    brokenfoot over 6 years
    +1. If the data we are talking about isn't complex, IMHO it is better to roll your own parser like in this ans.
  • Vinod Kumar Chauhan
    Vinod Kumar Chauhan over 4 years
    Exactly what I need. Thanks!