0

エンコードに問題があります。テキストファイルを読み取り、行を単語に分割してから、この単語を配列に入れる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
4

2 に答える 2

0

halaとその他を文字列に変換してみてください。関数を使用しますstr()

于 2013-02-21T13:31:57.463 に答える