Help with parsing a list

Discussion in 'Python' started by J, Dec 16, 2009.

  1. J

    J Guest

    Hi all,

    I need some help in turning a list into a dictionary...

    The list looks something like this:

    ['key1: data1','key2: data2','key3: data3',' key4: ',' \tdata4.1','
    \tdata4.2',' \tdata4.3','key5: data5']

    and it's derived from output (via subprocess.Popen) that in a terminal
    would look like this:

    key1: data1
    key2: data2
    key3: data3
    key4:
    data4.1
    data4.2
    data4.3
    key5: data5

    So what I want to do is turn this into a dictionary that looks like this:

    {'key1':'data1','key2':'data2','key3':'data3','key4':['data4.1','data4.2','data4.3'],'key5':'data5']

    So the problem I am having is just with the key 4 stuff... right now,
    I'm looking at something like this (pseudocode because I'm still
    trying to work this out)

    loop through list
    if item is 'flags'
    make flags the key
    add every following item until item + 1 does not start with \t
    partition item where item[0] is key and item[2] is value

    Sorry for not having actual code yet... I am still trying to work out
    how to handle this on paper before actually writing code.

    And no, it's not a homework assignment ;-) just a small part of a
    project I'm working on.

    Cheers,

    Jeff

    --

    Pablo Picasso - "Computers are useless. They can only give you
    answers." - http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html
     
    J, Dec 16, 2009
    #1
    1. Advertisements

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. roopa
    Replies:
    6
    Views:
    1,026
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    611
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    958
    Debajit Adhikary
    Oct 18, 2007
  4. Donn Ingle
    Replies:
    7
    Views:
    412
    Paul McGuire
    Nov 5, 2007
  5. Andrew Thompson

    Parsing M3U play list help

    Andrew Thompson, Apr 3, 2009, in forum: Java
    Replies:
    0
    Views:
    1,412
    Andrew Thompson
    Apr 3, 2009
  6. Emile van Sebille

    Re: Help with parsing a list

    Emile van Sebille, Dec 16, 2009, in forum: Python
    Replies:
    3
    Views:
    418
  7. OW Ghim Siong
    Replies:
    2
    Views:
    648
    Peter Otten
    Nov 30, 2010
  8. asil klin
    Replies:
    28
    Views:
    1,589
    Andreas Leitgeb
    Mar 5, 2011
Loading...