python - 获取 MYSQL 列值作为一个变量中的列表
问题描述
我希望列值作为列表输出
['Repair and Maintenance - General', 'Advance salary', 'EXIM Deposit', 'Office Cleaning Expenses']
但我只能获得这种类型的输出
[('Repair and Maintenance - General',), ('Advance salary',), ('EXIM Deposit',), ('Office Cleaning Expenses',)]
使用以下代码,任何人都可以帮助我
import mysql
import mysql.connector
import pandas as pd
conn = mysql.connector.connect(host="localhost", user="root",
passwd="Abcd11",database="entry",auth_plugin="mysql_native_password")
query= "select ledger from l_db_name"
cur=conn.cursor()
cur.execute(query)
rows=cur.fetchall()
conn.commit()
print(rows);
解决方案
使用列表理解提取每个元组的第一个元素
rows = cur.fetchall()
rows = [row[0] for row in rows]
推荐阅读
- square-connect - 在 api 中创建的方形订单未显示在实时仪表板中
- javascript - 如何同时运行 Django 和 React 服务器?
- javascript - 将 HTML 5 Canvas 序列化为 SVG 以发送到服务器
- kotlin - Tornadofx 过滤器输入
- firebase - 通过电子邮件/密码进行身份验证的颤振问题
- c# - 将 ExpandoObject 反序列化为 Object
- vb.net - vb.net - 服务应用程序和表单应用程序都具有非常高的 CPU 使用率,因为添加了套接字通信
- apache-camel - SFTP 传输:如何按名称过滤文件
- netsuite - 使用 RESTlet 在 NetSuite 中支付发票(全额支付)
- python - 在 BrowserStack 上 Selenium 等待失败