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

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    10

    Basic file parsing question


    I have a file in which the first line is an integer and the rest of the lines consist of a string of letters and spaces, a tab, and a float. When I get to the integer line, I want to remember the number, and with the other lines I want to split the line on the tab into a 2 element list. Here is my attempt at this:

    Code:
    def main(file):
        import re
        infile = open(file, 'r')
    
        for line in infile:
            line = line.rstrip('\n')
            total = 0
            if len(line) > 0: # I want to ignore blank lines
                line = eval(line)
                if re.match('^\d', line):
                    total = line
                else:
                    lst = line.split('\t')
            
        infile.close()
    Python doesn't seem to like the 'line = eval(line)'. In the shell, I can try using eval on two types of lines:

    Code:
    line = '4'
    eval(line)
    line = 'John Smith'
    eval(line)
    Python's fine with the first eval but not the second. So I have two questions:

    1. What is wrong with using eval on a string like 'John Smith'?

    2. How should I parse the file so that I treat the integer line one way and the rest of the (non-blank) lines a different way?

    Thank you.

    Eric
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    Code:
    >>> ######### eval requires a valid python expression
     >>> "'John Smith'"
    "'John Smith'"
    >>>
    >>> print('"John Smith"')
    "John Smith"
    >>>
    >>> eval("'John Smith'")
    'John Smith'
    >>>
    >>>
    >>> JohnSmith = 'JohnSmith is a variable with a global or local value'
    >>>
    >>> JohnSmith
    'JohnSmith is a variable with a global or local value'
    >>>
    >>> eval('JohnSmith')
    'JohnSmith is a variable with a global or local value'
    >>> 
    >>>
    >>> John Smith
      File "<stdin>", line 1
        John Smith
                 ^
    SyntaxError: invalid syntax
    >>>
    >>> 
    >>> eval('John Smith')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1
        John Smith
                 ^
    SyntaxError: unexpected EOF while parsing
    >>> 
    >>>
    >>> ################### string.split()
    >>> ################### index rightmost field
    >>> 'a b   c  de  f\t2.71828'.split('\t')
    ['a b   c  de  f', '2.71828']
    >>> import math
    >>> math.log(float('a b   c  de  f\t2.71828'.split('\t')[-1]))
    0.999999327347282
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    10
    Hi b49P23TIvg,

    I think I get it: eval expects a string. A string is enclosed in quotes. So eval will take what it's given, take off the outside quotes, and evaluate the expression, just as if I had typed it in that way in the shell. I can type in:

    'John Smith' (with the single quotes)

    in the shell, and that will evaluate to:

    'John Smith' (with the single quotes), telling me that I have entered in the string:

    'John Smith'

    But if I type in the shell:

    line = 'John Smith'
    eval(line)

    then Python will try to interpret essentially what would happen if I typed:

    John Smith (without quotes)

    in the shell. And that doesn't make any sense, since there are no variables named either John or Smith.

    Thanks very much for helping me out with this.

    Best wishes,

    Eric

IMN logo majestic logo threadwatch logo seochat tools logo