我从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", ....]]
所以,我怎么能这样做,因为分裂不能用于元组…谢谢!!

最佳答案

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]

关于python - 在Python中将元组拆分为所需的格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10211350/

10-09 21:03