gpt4 book ai didi

python - 在 python postgresql 列表中返回单列提取

转载 作者:行者123 更新时间:2023-12-05 03:38:49 24 4
gpt4 key购买 nike

数据库:

<表类="s-表"><头>id贸易 token <正文>1abc55232fdfd51453自给自足2899

代码:

def db_fetchquery(sql):
conn = psycopg2.connect(database="trade", user='postgres', password='jps', host='127.0.0.1', port= '5432')
cursor = conn.cursor()
conn.autocommit = True
cursor.execute(sql)
row = cursor.rowcount
if row >= 1:
data = cursor.fetchall()
conn.close()
return data
conn.close()
return False

print(db_fetchquery("SELECT token FROM script"))

结果:

[(5523,),(5145,),(2899,)]

但我需要如下结果:

[5523,5145,2899]

我也试过 print(db_fetchquery("SELECT zerodha FROM script")[0]) 但结果是:- [(5523,)]

另外,为什么我只抓取一列时列表中有','/列表?

最佳答案

不确定您是否能够在不进行进一步处理的情况下做到这一点,但我会这样做:

data = [x[0] for x in data]

将元组列表转换为一维列表

关于python - 在 python postgresql 列表中返回单列提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68842475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com