gpt4 book ai didi

Python 游标返回行数而不是行数

转载 作者:行者123 更新时间:2023-11-29 08:23:04 25 4
gpt4 key购买 nike

编写脚本来清理一些数据。 super 未优化,但这个光标是返回类似查询中的结果数而不是行数我做错了什么。

#!/usr/bin/python
import re
import MySQLdb
import collections

db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="admin", # your username
passwd="", # your password
db="test") # name of the data base

# you must create a Cursor object. It will let
# you execute all the query you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM vendor")

seen = []

# print all the first cell of all the rows
for row in cur.fetchall() :
for word in row[1].split(' '):
seen.append(word)

_digits = re.compile('\d')
def contains_digits(d):
return bool(_digits.search(d))


count_word = collections.Counter(seen)
found_multi = [i for i in count_word if count_word[i] > 1 and not contains_digits(i) and len(i) > 1]

unique_multiples = list(found_multi)

groups = dict()

for word in unique_multiples:
like_str = '%' + word + '%'
res = cur.execute("""SELECT * FROM vendor where name like %s""", like_str)

最佳答案

您正在存储 cur.execute() 的结果,即行数。您实际上从未获取任何结果。

执行后使用.fetchall()获取所有结果行或迭代游标:

for word in unique_multiples:
like_str = '%' + word + '%'
cur.execute("""SELECT * FROM vendor where name like %s""", like_str)
for row in cur:
print row

关于Python 游标返回行数而不是行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18774599/

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