pyrhon 技巧 解析pymysql查询结果,取mysql其中的某一列数据 zip解压

一般用python查询MySQL后,返回的结果是list或者tuple,如何取某一列数据呢?

mysql的返回值可以是tuple也可以是dict,常用的时tuple。

有时候想要根据前一个SQL的结果去生成另一个SQL,比如:

sql1 = select id,name from article limit 10;

sql2 = select * from reply where article_id in (sql1.id);

如是就有了下面的问题:

(('a',), ('b',), ('c',), ('d',), ('e',), ('f',)) 
到
'a','b','c','d','e','f'
怎么做到呢?

使用这个,不再拼接字符串:
>>> a=(('a',1), ('b',2), ('c',3), ('d',4), ('e',5), ('f',6))
>>> id,name = zip(*a)
>>> print str(id);
('a', 'b', 'c', 'd', 'e', 'f')
>>> print str(name);
(1, 2, 3, 4, 5, 6)

python  zip 解压 zip(*迭代对象)

zip(*a)

猜你喜欢

转载自www.cnblogs.com/cupleo/p/12325097.html