gpt4 book ai didi

Python访问具有动态获取的列名的单元格

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:07 25 4
gpt4 key购买 nike

我正在使用 pyodbc 连接到数据库,需要从某些表中提取信息。

我正在根据某些列名称的值为表中的每一行创建一个唯一的 ID。我正在从配置文件中获取用于 id 的每个列名称。

我可以用硬编码的列名来做到这一点:

connection = pyodbc.connect("connection string")
cursor = connection.cursor()
cursor.execute("select * from " + t) //t is the table name
rows = cursor.fetchall()
for row in rows:
id = `row.GroupID` + `row.Leg`

但是我怎样才能用我的配置文件中的名称替换列名称呢?

最佳答案

您可以使用getattr:

colname = "GroupID"
getattr(row, colname)

关于Python访问具有动态获取的列名的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053190/

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