我将FB post ID存储在sqlite表中。现在我想使用post_id
查询FB图中的其他数据。下面的黑客工作,但感觉非常好......
cur.execute("SELECT post_id FROM fbposts")
data_all = cur.fetchall()
for x in data_all:
y = "%s" % x
y
的值现在类似于96085205666_10153983162390667
,可用于构建新的FB api调用,但必须有更多的pythonic方式
答案 0 :(得分:4)
根据您的请求,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]
)
答案 1 :(得分:2)
使用以下模式怎么样:
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)