String from File -> List without parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor Horvath

    #1

    String from File -> List without parsing

    Hi,

    given the dynamic nature of python I assume that there is an elegant
    solution for my problem, but I did not manage to find it.

    I have a file that contains for example on line:

    ['147', '148', '146']

    when I read the file

    f = file("I050901.i ds").readlines( )

    I have a string

    f[0] == "['147', '148', '146']"

    How can I turn this string into a list

    li == ['147', '148', '146']

    without parsing?

    --
    Greg
  • Jean-François Doyon

    #2
    Re: String from File -> List without parsing

    Gregor,

    You want to use eval():

    Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
    win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> eval('[3,54,5]')[/color][/color][/color]
    [3, 54, 5][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Cheers,
    J.F.

    Gregor Horvath wrote:[color=blue]
    > Hi,
    >
    > given the dynamic nature of python I assume that there is an elegant
    > solution for my problem, but I did not manage to find it.
    >
    > I have a file that contains for example on line:
    >
    > ['147', '148', '146']
    >
    > when I read the file
    >
    > f = file("I050901.i ds").readlines( )
    >
    > I have a string
    >
    > f[0] == "['147', '148', '146']"
    >
    > How can I turn this string into a list
    >
    > li == ['147', '148', '146']
    >
    > without parsing?
    >
    > --
    > Greg[/color]

    Comment

    • John Machin

      #3
      Re: String from File -> List without parsing

      Gregor Horvath wrote:[color=blue]
      > Hi,
      >
      > given the dynamic nature of python I assume that there is an elegant
      > solution for my problem, but I did not manage to find it.
      >
      > I have a file that contains for example on line:
      > ['147', '148', '146']
      > when I read the file
      > f = file("I050901.i ds").readlines( )[/color]

      Y3K bug alert :-)
      [color=blue]
      > I have a string
      > f[0] == "['147', '148', '146']"[/color]

      The last (or sole) line is highly likely to be "['147', '148',
      '146']\n". If there are multiple lines in the file, all but the last
      will definitely have "\n" at the end.
      [color=blue]
      > How can I turn this string into a list
      > li == ['147', '148', '146']
      > without parsing?[/color]

      Something like this:
      [color=blue][color=green][color=darkred]
      >>> def munch(astrg):[/color][/color][/color]
      .... return [x[1:-1] for x in astrg.rstrip("\ n")[1:-1].split(", ")]
      ....[color=blue][color=green][color=darkred]
      >>> munch("['147', '148', '146']")[/color][/color][/color]
      ['147', '148', '146'][color=blue][color=green][color=darkred]
      >>> munch("['147', '148', '146']\n")[/color][/color][/color]
      ['147', '148', '146'][color=blue][color=green][color=darkred]
      >>> munch("['147']\n")[/color][/color][/color]
      ['147'][color=blue][color=green][color=darkred]
      >>> # Warning: various flavours of "nothing" give the same result:
      >>> munch("['']\n")[/color][/color][/color]
      [''][color=blue][color=green][color=darkred]
      >>> munch("[]\n")[/color][/color][/color]
      [''][color=blue][color=green][color=darkred]
      >>> munch("\n")[/color][/color][/color]
      [''][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      This assumes that the contents are no more complicated than in your example.

      Some notes:

      (1) You neither want nor need to use eval.

      (2) What is creating files like that? If it is a Python script, consider
      writing them using the csv module; that way, other software can read
      them easily.

      HTH,
      John

      Comment

      • Gregor Horvath

        #4
        Re: String from File -> List without parsing

        John Machin wrote:
        [color=blue][color=green]
        >> f = file("I050901.i ds").readlines( )[/color]
        >
        > Y3K bug alert :-)[/color]

        but then there is Python 3000 and Hurd, which solves all problems of
        this universe :-)
        [color=blue]
        > Something like this:
        >[color=green][color=darkred]
        > >>> def munch(astrg):[/color][/color]
        > ... return [x[1:-1] for x in astrg.rstrip("\ n")[1:-1].split(", ")][/color]

        Thanks!
        [color=blue]
        > (1) You neither want nor need to use eval.[/color]

        I wanted to avoid parsing, but when parsing is an 1 liner, I think its
        the better solution.
        [color=blue]
        >
        > (2) What is creating files like that? If it is a Python script, consider
        > writing them using the csv module; that way, other software can read
        > them easily.
        >[/color]

        Its a Python script.
        But the file is not intended to be used by another sotware, although it
        should be readable by humans. I think the python syntax is better human
        readable than csv.

        After all the sense of this file is to make a list persistant quickly
        and easily and human readable. Maybe pickleing is a third option?

        --
        Gregor

        Comment

        Working...