![](/img/trans.png)
[英]is there a more 'pythonic' way to generate a string from a list with customized separators
[英]more pythonic way to reveal string from sqlite buffer
我将FB帖子ID存储在sqlite表中。 现在,我想使用post_id
查询FB图中的其他数据。 下面的hack可以工作,但是感觉很傻。
cur.execute("SELECT post_id FROM fbposts")
data_all = cur.fetchall()
for x in data_all:
y = "%s" % x
y
的值现在类似于96085205666_10153983162390667
,可以用来构造新的FB api调用,但是必须有一种更Python的方式
根据您的要求, data_all
可在1个元素的元组上进行迭代
`y = "%s" % x` properly converts a 1-element tuple to a string, but you're right, it's not the best way.
fetchall():获取查询结果的所有(剩余)行,并返回一个列表。 请注意,游标的arraysize属性会影响此操作的性能。 如果没有可用的行,则返回一个空列表。
要获取此元素并使用字符串创建列表,请执行以下操作:
[x for (x,) in data_all]
这会在元组中解包x
并创建字符串列表(相当于[x[0] for x in data_all]
)
如何使用以下模式:
cur.execute("SELECT post_id FROM fbposts")
data_all = [item[0] for item in cur.fetchall()]
现在,您的data_all将是所需的字符串列表。 而且,如果您要为每个post_id拨打电话,现在可以执行以下操作:
for post_id in data_all:
fb.call(post_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.