Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with parsing a list

1 view
Skip to first unread message

J

unread,
Dec 16, 2009, 4:32:57 PM12/16/09
to pytho...@python.org
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

Emile van Sebille

unread,
Dec 16, 2009, 6:05:42 PM12/16/09
to pytho...@python.org
On 12/16/2009 1:32 PM J said...

> 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']

Sometimes brute force works fine...

>>> data = ['key1: data1','key2: data2','key3: data3',' key4: ','

\tdata4.1','\tdata4.2',' \tdata4.3','key5: data5']
>>>

>>> D = {}
>>>
>>> for datum in data:
... parts = [ d.strip() for d in datum.split(":") ]
... if len(parts) == 2:
... if not(parts[1]):
... priorList = []
... D[parts[0]] = priorList
... else:
... D[parts[0]] = parts[1]
... else:
... priorList.append(parts[0])
...
>>> D
{'key3': 'data3', 'key2': 'data2', 'key1': 'data1', 'key5': 'data5',

'key4': ['data4.1', 'data4.2', 'data4.3']}
>>>


Flavor to taste,

Emile

MRAB

unread,
Dec 16, 2009, 6:21:05 PM12/16/09
to pytho...@python.org
J wrote:
> 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.
>
Speaking personally, I'd make all of the values lists, instead of some
them lists and others strings.

You could split each list item into a key/value pair. Some items would
have a key and an empty value, eg. ['key4', ''], some only a value, eg.
['data4.1']. If there's a key then add the key and an empty list to the
dict; if there's a value then add it to the list for the key; if there's
no key then use the previous key.

thund...@gmail.com

unread,
Dec 16, 2009, 6:23:41 PM12/16/09
to
not as slick as Emile's (didn't think about using strip() ), but
seemingly functional:


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

result = {}

for item in data:
if item.endswith(': '):
currkey = item[:-2]
result[currkey] = []
elif item.startswith(' \t'):
result[currkey].append(item[2:])
else:
key, val = item.split(': ')
result[key] = val

print 'data = %s' % data
print 'result = %s' % result

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

result = {'key3': 'data3', 'key2': 'data2', 'key1': 'data1', 'key5':

Sallu

unread,
Dec 17, 2009, 7:05:13 AM12/17/09
to
On Dec 17, 4:23 am, "thunderf...@gmail.com" <thunderf...@gmail.com>
wrote:

Hi i tried with thunderfoot code

error:

Traceback (most recent call last):
File "<stdin>", line 8, in ?
ValueError: need more than 1 value to unpack

thund...@gmail.com

unread,
Dec 17, 2009, 12:10:49 PM12/17/09
to
On Dec 17, 6:05 am, Sallu <praveen.sunsetpo...@gmail.com> wrote:
> Hi i tried with thunderfoot code
>
> error:
>
> Traceback (most recent call last):
>   File "<stdin>", line 8, in ?
> ValueError: need more than 1 value to unpack- Hide quoted text -
>

hence, my 'seemingly' functional qualification. :)

that's most likely to due to a datum starting with '\t' instead of '
\t'. yet another reason that emile's code is superior to my one off.

0 new messages