gpt4 book ai didi

python - 连接表的多个列

转载 作者:行者123 更新时间:2023-11-29 12:58:19 25 4
gpt4 key购买 nike

  • 我正在编写代码,用于从 MySQL 的多个列中获取数据数据库。
  • 在我的编码中,我需要打印我的代码中存在的行数据库,我做到了,它工作正常,但它不打印下一个列单词匹配。
  • 我有一个文本文件“qwer.txt”,其中包含LINE1:“我有一辆汽车”.LINE2:“我有一部电话”
  • 我的tablename'adarsh1',其中“A1”列由car组成“A2” 由电话组成。

根据我的编码,它打印 “我有一辆汽车” 而不是 “我有一辆汽车” “I有电话”

那么,我的编码中存在什么问题,阻止我打印这两行,因为它出现在表格的两列中?

<小时/>
import MySQLdb


db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="mysql", # your password
db="sakila") # name of the data base
cursor = db.cursor()

# execute SQL select statement
cursor.execute("SELECT A1,A2 FROM adarsh1")


# commit your changes
db.commit()

keywords=[]
#here fetchall() gets all the rows and we append carnames to key words

for i in cursor.fetchall():
keywords.append(i[1])

with open('qwer.txt','r') as file:
for line in file:
for key in keywords:
if key in line:
print line

最佳答案

您仅向关键字附加一列。您可能应该附加 i[0] 和 i[1]:

    import MySQLdb


db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="mysql", # your password
db="sakila") # name of the data base
cursor = db.cursor()

# execute SQL select statement
cursor.execute("SELECT A1,A2 FROM adarsh1")


# commit your changes
db.commit()

keywords=[]
#here fetchall() gets all the rows and we append carnames to key words

for i in cursor.fetchall():
keywords.append(i[0])
keywords.append(i[1])

with open('qwer.txt','r') as file:
for line in file:
for key in keywords:
if key in line:
print line

关于python - 连接表的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710572/

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