Generic string import like in strptime?

Discussion in 'Python' started by Andre, Jan 16, 2008.

  1. Andre

    Andre Guest

    Hi there

    Is there a function like strptime, which takes a string and converts it
    into an array depending on a format string I provide. Like:
    >>> a = '3456\tblub-blib.0.9'
    >>> b = '%d\t%s-%s.%f'
    >>> c = mysticalfunction(a,b)
    >>> print c

    [3456,'blub','blib',0.9]


    Many thanks

    Andre
    Andre, Jan 16, 2008
    #1
    1. Advertising

  2. Andre

    Neil Cerutti Guest

    On Jan 16, 2008 3:34 AM, Andre <-chemnitz.de> wrote:
    > Hi there
    >
    > Is there a function like strptime, which takes a string and converts it
    > into an array depending on a format string I provide. Like:
    > >>> a = '3456\tblub-blib.0.9'
    > >>> b = '%d\t%s-%s.%f'
    > >>> c = mysticalfunction(a,b)
    > >>> print c

    > [3456,'blub','blib',0.9]


    No, not in the standard distribution of Python.

    In Python, you're expected to use appropriate string methods, or hold
    your nose and drag out the re module. There are some scanf-like
    libraries for Python available on the net, e.g.,
    http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/. None of them have
    become popular enough with Python users to make it into the standard
    distribution.

    An excellent tool that can be used in these cases is pyparsing, which
    is also not in the standard distribution.
    http://pyparsing.wikispaces.com/

    --
    Neil Cerutti <>
    Neil Cerutti, Jan 16, 2008
    #2
    1. Advertising

  3. Andre

    Paul Hankin Guest

    On Jan 16, 8:34 am, Andre <-chemnitz.de> wrote:
    > Hi there
    >
    > Is there a function like strptime, which takes a string and converts it
    > into an array depending on a format string I provide. Like:>>> a = '3456\tblub-blib.0.9'
    > >>> b = '%d\t%s-%s.%f'
    > >>> c = mysticalfunction(a,b)
    > >>> print c

    >
    > [3456,'blub','blib',0.9]


    Use regular expressions: see http://docs.python.org/lib/node49.html

    --
    Paul Hankin
    Paul Hankin, Jan 16, 2008
    #3
  4. Andre

    Andre' John Guest

    Nice. Thanks a lot.

    Andre


    On Wed, 16 Jan 2008, Paul Hankin wrote:

    > On Jan 16, 8:34 am, Andre <-chemnitz.de> wrote:
    > > Hi there
    > >
    > > Is there a function like strptime, which takes a string and converts it
    > > into an array depending on a format string I provide. Like:>>> a = '3456\tblub-blib.0.9'
    > > >>> b = '%d\t%s-%s.%f'
    > > >>> c = mysticalfunction(a,b)
    > > >>> print c

    > >
    > > [3456,'blub','blib',0.9]

    >
    > Use regular expressions: see http://docs.python.org/lib/node49.html
    >
    > --
    > Paul Hankin
    >
    Andre' John, Jan 16, 2008
    #4
    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. Murat Tasan
    Replies:
    1
    Views:
    8,037
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    432
  3. Robert
    Replies:
    2
    Views:
    278
    Fredrik Lundh
    Sep 13, 2005
  4. kongkolvyu
    Replies:
    4
    Views:
    486
    Old Wolf
    Jan 4, 2006
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page