2012-04-18 81 views
3

所要求的格式,我讀了sqlite3的數據,並輸出類似 (3252, u'https://www.google.fr/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+split+tuple', u'Using the split command in a list - Python', 10)拆分元組在Python

所以我想將它重新格式化爲像一個表: table = [["", "number", "url", "title"], ["number", 3252], ["urls", .....], ["title", ....]] 所以,我怎麼能做出這樣這是因爲分割不能用於元組......謝謝!

+0

從我個人理解,你想創建詞典嗎? – hjpotter92 2012-04-18 14:21:08

回答

0
keys = ["number", "url", "title"] 
table = [zip(keys, tup) for tup in lines if len(tup) == 3] 

,如果你想把它當作字典:

table = [dict(zip(keys, tup)) for tup in lines if len(tup) == 3]