Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Saving/loading list of strings to/from a file.

LobomoonLobomoon Posts: 7Member
Could you show me how to write a Python code that would create a list (myList) that has 100 strings in it (each one is "word"), save that list's values to a text file (myList.txt -create it if it doesn't exist) and load that list values from that text file (but check if the file exist first). Thank you.

Comments

  • infidelinfidel Posts: 2,900Member
    : Could you show me how to write a Python code that would create a list (myList) that has 100 strings in it (each one is "word"), save that list's values to a text file (myList.txt -create it if it doesn't exist) and load that list values from that text file (but check if the file exist first). Thank you.

    Is this an assignment for a class? Sounds like it.

    There are a few ways to create your list. Here is the easiest:

    [code]
    >>> myList = ['word'] * 100
    [/code]

    There's also a more direct approach:

    [code]
    >>> myList = []
    >>> for x in range(100):
    ... myList.append('word')
    ...
    [/code]

    To save this list to a file, do you want to pickle it or just save 100 lines, each with the word 'word' in them? Pickling will save the whole list in a format that can then be converted back into a list object.

    To open a file for reading, do this:

    [code]
    >>> f = open('path','r')
    [/code]

    Now you can f.read() or f.readlines() depending on what you want.

    If you want to check if the file exists before opening it, you should read the documentation for the os module. I know there's something in there that will help but it's not my project so I'm not going to bother looking for it myself.

    If you want to do something more Pythonic, use a try block:

    [code]
    >>> try:
    ... f = file('foo.txt','r')
    ... except IOError, e:
    ... print e
    ... else:
    ... print f.read()
    ...
    [/code]



    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • LobomoonLobomoon Posts: 7Member
    I guess I better save myList one string per line because some string could have more than one word in them and the spaces between them. So what would be the Python code to save myList to myList.txt one element per line and load it later from such a file?

    Thank you.
  • infidelinfidel Posts: 2,900Member
    : I guess I better save myList one string per line because some string could have more than one word in them and the spaces between them. So what would be the Python code to save myList to myList.txt one element per line and load it later from such a file?

    Well, to open a file for writing, you do:

    f = file('c:\foo.txt', 'w')

    Now, in order to write multiple lines, you'll need to put the newline characters in yourself. If you've got a list of strings, the easiest way to do this is:

    text = '
    '.join(myList)

    Now text will be a string consisting of all the strings in myList joined together by newlines.

    Then you just

    f.write(text)

    To read lines from a file, just do:

    f = file('c:\foo.txt', 'r')
    myList = f.readlines()

    Bear in mind that .readlines leaves the newline characters at the end of each list item so if you re-write myList back to a file you don't want to put more in.



    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • LobomoonLobomoon Posts: 7Member
    Thanks. I got few mo questions:

    1. What's the easiest way to remove "
    " from the end of the string in the list. For example when I type:

    print "Is your name", myList[25],"?"

    it puts "?" on the new line which I don't want.

    2. Can I pickle more than one element to one file (like for example 2 lists of strings, a list of integers and an integer)?

    3. Can I make standalone executable?
  • infidelinfidel Posts: 2,900Member
    : Thanks. I got few mo questions:
    :
    : 1. What's the easiest way to remove "
    " from the end of the string in the list. For example when I type:
    :
    : print "Is your name", myList[25],"?"

    Use slice notation:

    print "Is your name", myList[25][:-1],"?"

    Note that there will be a space between the string and your question mark. You should concatenate them together like this:

    print "Is your name", myList[25][:-1] + "?"

    Or use string formatting like this:

    print "Is your name %s?" % (myList[25][:-1])

    : 2. Can I pickle more than one element to one file (like for example 2 lists of strings, a list of integers and an integer)?

    Yes, but you have to use the shelve module.

    : 3. Can I make standalone executable?

    Not normally. There are, however, a couple of third-party tools that will combine the interpreter and your scripts into an exe, but I've never tried them. I think Computronix makes one called cx_Freeze or something like that.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.