首页 > 解决方案 > 获取 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);

标签: pythonmysql

解决方案


使用列表理解提取每个元组的第一个元素

rows = cur.fetchall()
rows = [row[0] for row in rows]

推荐阅读