在Python中将元组拆分为所需的格式

时间:2012-04-18 14:19:03

标签: python sqlite tuples

我从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)

所以我想将它重新格式化为如下表: table = [["", "number", "url", "title"], ["number", 3252], ["urls", .....], ["title", ....]] 所以,我怎么能这样做,因为拆分不能用于元组...谢谢!!

1 个答案:

答案 0 :(得分: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]