我的编码有问题。我编写了一个 python 代码,它读取一个文本文件并将行拆分为单词,然后将这些单词放入数组中。这是我的代码:
from whoosh import fields, index
import os.path
import csv
import codecs
# This list associates a name with each position in a row
columns = ["juza","chapter","verse","voc"]
schema = fields.Schema(juza=fields.NUMERIC,
chapter=fields.NUMERIC,
verse=fields.NUMERIC,
voc=fields.TEXT)
# Create the Whoosh index
indexname = "indexdir"
if not os.path.exists(indexname):
os.mkdir(indexname)
ix = index.create_in(indexname, schema)
# Open a writer for the index
with ix.writer() as writer:
# Open the CSV file
with open("tt.txt", 'r') as txtfile:
# Create a csv reader object for the file
#csvreader = csv.reader(csvfile,delimiter='\t')
lines=txtfile.readlines()
# Read each row in the file
for i in lines:
# Create a dictionary to hold the document values for this row
doc = {}
thisline=i.split()
u=0
# Read the values for the row enumerated like
# (0, "juza"), (1, "chapter"), etc.
for w in thisline:
# Get the field name from the "columns" list
fieldname = columns[u]
type(w)
u+=1
# Strip any whitespace and convert to unicode
# NOTE: you need to pass the right encoding here!
#value = unicode(value.strip(), "utf-8")
# Put the value in the dictionary
doc[fieldname] =u"w"
# Pass the dictionary to the add_document method
writer.add_document(**doc)
writer.commit()
但我面临这个问题:
Traceback (most recent call last):
File "C:\Python27\yarab.py", line 53, in <module>
writer.add_document(**doc)
File "C:\Python27\whoosh\filedb\filewriting.py", line 369, in add_document
items = field.index(value)
File "C:\Python27\whoosh\fields.py", line 466, in index
return [(txt, 1, 1.0, '') for txt in self._tiers(num)]
File "C:\Python27\whoosh\fields.py", line 454, in _tiers
yield self.to_text(num, shift=shift)
File "C:\Python27\whoosh\fields.py", line 487, in to_text
return self._to_text(self.prepare_number(x), shift=shift,
File "C:\Python27\whoosh\fields.py", line 476, in prepare_number
x = self.type(x)
ValueError: invalid literal for int() with base 10: 'w'
这是输入文本文件的示例:
1 1 1 hala
1 1 2 yomna
1 1 3 hala
1 1 4 yomna
1 2 1 hala
1 2 2 yoomna