gpt4 book ai didi

python - Writer.add_document() 函数错误 Whoosh - mysql 循环

转载 作者:行者123 更新时间:2023-11-28 23:56:01 34 4
gpt4 key购买 nike

我正在尝试从以 latin1 编码的数据库中索引大量文章。我已经解决了字符集的编码问题,但我无法将每一行添加到索引中。

我试过:1)

writer.add_document(Id = unicode(row["Id"]),Body = unicode(row["Body"]), Name = unicode(row["Name"]), Brand = unicode(row["Brand"]), Familia = unicode(row["Familia"]))    

这索引文档但不尊重索引标签。

2)

writer.add_document(doc)

此报告 add_document() 正好接受 1 个参数(给定 2 个)错误

完整代码:

# Open a writer for the index
with ix.writer() as writer:

con= mdb.connect(host="myhost",
user="myuser",
passwd="pass",
db="db",
charset="utf8",
use_unicode=True)
with con:

cur = con.cursor(mdb.cursors.DictCursor)
#cur.execute("SELECT Id, Body, Name, Brand, Familia FROM articles")
rows = cur.fetchall()
for row in rows:
print row
doc6 = row["Brand"]
doc2 = row["Name"]
print doc2
print 'body'
doc3 = row["Body"].replace("á", "a")
doc3 = doc3.replace("é", "e")
doc3 = doc3.replace("í", "i")
doc3 = doc3.replace("ó", "o")
doc3 = doc3.replace("ú", "u")
doc3 = doc3.replace("ñ", "n")
doc3 = doc3.replace(""", "")
print doc3
print 'familia'
doc4 = row["Familia"]
print doc4
print 'id'
doc5 = row["Id"]
print doc5

writer.add_document(Id = unicode(row["Id"]),Body = unicode(row["Body"]), Name = unicode(row["Name"]), Brand = unicode(row["Brand"]), Familia = unicode(row["Familia"]))
#
# doc = unicode(doc5),unicode(doc3), unicode(doc2), unicode(doc6), unicode(doc4)
# writer.add_document(doc) #reports add_document() takes exactly 1 argument (2 given) Error
#writer.add_document(Id = unicode(doc5),Body = unicode(doc3), Name = unicode(doc2), Brand = unicode(doc6), Familia = unicode(doc4))


numdocs = ix.doc_count_all()
print "docs indexed =", numdocs

提前谢谢大家!

最佳答案

这样解决的:

with con:

cur = con.cursor(mdb.cursors.DictCursor)
#cur.execute("SELECT Id, Body, Name, Brand, Familia FROM articles")
rows = cur.fetchall()
for row in rows:
#print row
row["Body"]= row["Body"].replace("á", "a")
row["Body"]= row["Body"].replace("é", "e")
row["Body"]= row["Body"].replace("í", "i")
row["Body"]= row["Body"].replace("ó", "o")
row["Body"]= row["Body"].replace("ú", "u")
row["Body"]= row["Body"].replace("ñ", "n")
row["Body"]= row["Body"].replace(""", "")

writer.add_document(Id=unicode(row["Id"]),
Body=unicode(row["Body"]),
Name=unicode(row["Name"]),
Brand=unicode(row["Brand"]),
Familia=unicode(row["Familia"]),
Relevancia=row["Relevancia"])

numdocs = ix.doc_count_all()
print "docs indexed =", numdocs

特别感谢Whoosh团队,耐心友好的解决了我所有的疑惑。

关于python - Writer.add_document() 函数错误 Whoosh - mysql 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31651906/

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