#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Mysore, India
    Posts
    2
    Rep Power
    0

    Converting a string into a list


    Hi,

    I am Madhukar, doing M.Sc in physics. My friends, and myself are automating a complex calculation in Physics using Python. I am new to programming, and this is my first programming project.

    We get a data file from a machine which contains thousands of numbers. Our program should read the data into a list in order to proceed with further calculations. I know how to read the file into a string, but I want the data to be in a list, as I may have to sort it first. For this I used the following code:

    file = open('data.txt')
    string = file.read() # Read the data into the string 'string'
    list = eval(string) #Got this line from the forum itslef.

    But this is showing the following message:
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 2
    3
    ^
    SyntaxError: invalid syntax


    As of now, I am not very clear about the eval(). Is there a simpler way (may be a bit longer) to convert a string into a list?

    Saw a quite a few posts on 'How to ask a question?' Have not been able to go through all of them. Will see them, and try to follow. Hope this is not a very bad post!

    Thanks!
    Madhukar
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    522
    Rep Power
    34
    Frist, you shouldn't use "file" as a variable name. It is a reserved word. To read into a list use
    data_list = open("data.txt", "r").readlines()
    print len(data_list), "records read"
    Using one line to open and read the file will cause to file to be closed on the next garbage collection, so it is both easier and cleaner. If you want to ask about handling the data, please include some sample data both before and the after, that is the desired result.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    eval() runs Python code. So if your text file does not look like the Python code for a list, eval() wont be able to make sense of it.

    The answer to your question depends on whether the text file is structured like:

    Code:
    1, 2, 3, 4, 5
    or

    Code:
    1
    2
    3
    4
    5
    or some other way. The readlines() method of an open file will get you a list where each item is a line in the file.

    When read from a file, they will be text, and you will also need to convert them to be numbers.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Assuming your string is like this: "1, 2, 3, 4, 5", you can split it like this:
    Code:
    mystring = myfile.read()
    mylist = mystring.split(", ")
    # Now mylist is a list of various items of the string.
    Don't use "list" as a variable, as it is a python keyword. Also, you might want to use mystring instead of string, so that you avoid confusion with the string module.

    You can change the argument to mystring.split() according to whatever your separator is (if it isn't ", ")
    Last edited by Scorpions4ever; May 27th, 2009 at 05:04 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Mysore, India
    Posts
    2
    Rep Power
    0

    Text to numbers: How?


    Thanks for the reply.

    My data file has numbers 1 to 10 arranged randomly. As you correctly mentioned, the numbers are being treated as text. I googled for "converting text into numbers in python", but could not solve my problem (was my query wrong?). Also, I referred O'Reilly - Learning Python, but in vain.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0
    Originally Posted by M Putty
    Thanks for the reply.

    My data file has numbers 1 to 10 arranged randomly. As you correctly mentioned, the numbers are being treated as text. I googled for "converting text into numbers in python", but could not solve my problem (was my query wrong?). Also, I referred O'Reilly - Learning Python, but in vain.
    This isn't a difficult problem at all, but no one will be able to give you a conclusive solution unless you show us what the data file looks like.

    EDIT:
    Well maybe I was too hasty to make that claim. Give this a shot:
    Code:
    import re 
    
    data = open('data.txt').read()
    
    number_list = [float(number) for number in re.findall('[0-9.]+', data)]
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    string text file to list - Need a little help


    Originally Posted by Scorpions4ever
    Assuming your string is like this: "1, 2, 3, 4, 5", you can split it like this:
    Code:
    mystring = myfile.read()
    mylist = mystring.split(", ")
    # Now mylist is a list of various items of the string.
    Don't use "list" as a variable, as it is a python keyword. Also, you might want to use mystring instead of string, so that you avoid confusion with the string module.

    You can change the argument to mystring.split() according to whatever your separator is (if it isn't ", ")
    I can't seem to make this work. Below is a simple version of my text. What would be the argument in mysplit() to make this work. Any other simple solution would be greatly appreciated!
    Thank you.

    mylist = ['apple', 'bear', 'cat']
    mystring = str(mylist)

    myfile = open("mytextfile", "w")
    myfile.write(mystring)
    myfile.close()

    myfile = open("mytextfile", "r")
    mystring = myfile.read()
    print("mystring = ", mystring)

    mylist2 = mystring.split('['', ]')
    myfile.close()

    print (mylist2)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    I don’t know why you thought it appropriate to resurrect a long-dead thread, but:

    Originally Posted by ackpython
    What would be the argument in mysplit() to make this work.
    By “mysplit()” you probably mean “mystring.split()”. In case you really have a file that’s in this rather peculiar format, you should proceed in two or three steps: first strip brackets from the line, then split the line, and lastly, if necessary, strip quotes from each element in the list. And actually even before the first step you should right-strip the line ending chars from the string read from a file.

    Code:
    mylist2 = [item.strip("'") for item in mystring.rstrip().strip('[]').split(', ')]
    But in actual practice you probably wouldn’t encounter this kind of formatting, and if you did, you might even – cautiously! – employ Python’s own parser to do this.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Thank you - Works great


    Originally Posted by SuperOscar
    I don’t know why you thought it appropriate to resurrect a long-dead thread, but:



    By “mysplit()” you probably mean “mystring.split()”. In case you really have a file that’s in this rather peculiar format, you should proceed in two or three steps: first strip brackets from the line, then split the line, and lastly, if necessary, strip quotes from each element in the list. And actually even before the first step you should right-strip the line ending chars from the string read from a file.

    Code:
    mylist2 = [item.strip("'") for item in mystring.rstrip().strip('[]').split(', ')]
    But in actual practice you probably wouldn’t encounter this kind of formatting, and if you did, you might even – cautiously! – employ Python’s own parser to do this.
    SuperOscar,
    First I would like to thank you for your quick reply to my request. Second, I was able to make your code suggestion work on my program - the actual one - not just the simply one I sent in.
    I appreciate you and this forum!!
    ackpython

IMN logo majestic logo threadwatch logo seochat tools logo