Re: Help with parsing a list

2009-12-16 Thread thunderf...@gmail.com
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':
'data5', ' key4': ['data4.1', 'data4.2', 'data4.3']}
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with parsing a list

2009-12-17 Thread thunderf...@gmail.com
On Dec 17, 6:05 am, Sallu  wrote:
> Hi i tried with thunderfoot code
>
> error:
>
> Traceback (most recent call last):
>   File "", 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.
-- 
http://mail.python.org/mailman/listinfo/python-list