0

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

#!/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)
4

1 回答 1

2

您正在存储 的结果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
于 2013-09-12T21:34:41.987 回答