Re: String to sequence

Discussion in 'Python' started by Peter Otten, Mar 14, 2009.

  1. Peter Otten

    Peter Otten Guest

    mattia wrote:

    > How can I convert the following string:
    >
    > 'AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
    > EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'
    >
    > into this sequence:
    >
    > ['AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
    > EGC','SXF','BZR','BIQ','BLL','BHX','BLQ']


    >>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'"
    >>> csv.reader(StringIO.StringIO(s), quotechar="'").next()

    ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']

    or

    >>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'"
    >>> list(compile(s, "nofile", "eval").co_consts[-1])

    ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']

    Peter
     
    Peter Otten, Mar 14, 2009
    #1
    1. Advertising

  2. Peter Otten

    Peter Otten Guest

    mattia wrote:

    > Il Sat, 14 Mar 2009 10:35:59 +0100, Peter Otten ha scritto:
    >
    >> mattia wrote:
    >>
    >>> How can I convert the following string:
    >>>
    >>> 'AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
    >>> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'
    >>>
    >>> into this sequence:
    >>>
    >>> ['AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
    >>> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ']

    >>
    >>>>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'"
    >>>>> csv.reader(StringIO.StringIO(s), quotechar="'").next()

    >> ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']
    >>
    >> or
    >>
    >>>>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'" list(compile(s,
    >>>>> "nofile", "eval").co_consts[-1])

    >> ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']
    >>
    >> Peter

    >
    > Ok, and what about if the string is "['AAR', 'ABZ', 'AGA', 'AHO', 'ALC']"
    > I wanted to use eval(string) but it is discouraged, they say.


    If you use the csv module you can remove the [] manually

    assert s.startswith("[")
    assert s.endswith("]")
    s = s[1:-1]

    compile() will work without the enclosing list(...) call.

    Yet another one is

    flights = re.compile("'([A-Z]+)'").findall(s)
    if any(len(f) != 3 for f in flights):
    raise ValueError

    Peter
     
    Peter Otten, Mar 14, 2009
    #2
    1. Advertising

  3. Peter Otten

    S Arrowsmith Guest

    Peter Otten <> wrote:
    >assert s.startswith("[")
    >assert s.endswith("]")
    >s = s[1:-1]


    s.strip('[]')

    (I suppose it all depends on how much you can trust the consistency
    of the input.)

    --
    \S

    under construction
     
    S Arrowsmith, Mar 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick.O.Ige
    Replies:
    1
    Views:
    1,995
    Patrick.O.Ige
    Jul 2, 2006
  2. Patrick.O.Ige
    Replies:
    4
    Views:
    2,725
    Patrick.O.Ige
    Jul 5, 2006
  3. Jim Langston

    escape sequence in string

    Jim Langston, Nov 21, 2006, in forum: C++
    Replies:
    4
    Views:
    335
    Jim Langston
    Nov 22, 2006
  4. stef mientki
    Replies:
    13
    Views:
    674
    stef mientki
    Oct 20, 2007
  5. Replies:
    10
    Views:
    698
    Steven D'Aprano
    Dec 16, 2007
Loading...

Share This Page