python中的索引不足

python中的索引不足,python,whoosh,Python,Whoosh,我试图对excel文件进行索引,我使用了whoosh软件包,但是,我 发现列表索引超出范围的错误。 请问,有人能帮我吗? 我的代码是: 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","analysis"] s

我试图对excel文件进行索引,我使用了whoosh软件包,但是,我 发现列表索引超出范围的错误。 请问,有人能帮我吗? 我的代码是:

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","analysis"]

schema = fields.Schema(juza=fields.NUMERIC,
                       chapter=fields.NUMERIC,
                       verse=fields.NUMERIC,
                       analysis=fields.KEYWORD)


# Create the Whoosh index
indexname = "index"
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 codecs.open("yom.csv", "rb","utf8") as csvfile:
    # Create a csv reader object for the file
    csvreader = csv.reader(csvfile)

    # Read each row in the file
    for row in csvreader:

      # Create a dictionary to hold the document values for this row
      doc = {}

      # Read the values for the row enumerated like
      # (0, "juza"), (1, "chapter"), etc.
      for colnum, value in enumerate(row):

        # Get the field name from the "columns" list
        fieldname = columns[colnum]

        # 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] = value

      # Pass the dictionary to the add_document method
      writer.add_document(**doc)
    writer.commit()
`
我得到这个错误,我不知道为什么? 错误:


csv.reader
使用逗号作为默认分隔符:

您必须明确定义分隔符:

csvreader = csv.reader(csvfile, delimiter=...)
但是,您的CSV文件不是同质的。阅读时最好不要使用
csv

columns = ["juza","chapter","verse","analysis"]
with codecs.open("yom.csv", "rb","utf8") as f:
    for line in f:
        a, b, c, rest = line.split('   ', 3)
        doc = {k:v.strip() for k,v in zip(columns, rest.split(':'))}
        # a,b,c are the first three integers
        # doc is a dictionary

你的意思是我应该删除“csvreader”并用你建议的代码替换它吗?但是如果我这样做,现在的问题是如何在下面的行中显示字段名:“fieldname=columns[colnum]”@user2091683-您不再需要
colnum
zip(columns,rest.split(“:”)
将它们“打包”在一起,并且
doc
-字典包含整个条目。这是我的新代码,请打开此链接:()此错误发生:回溯(最近一次调用):文件“C:\Python27\yarab.py”,第26行,在juza,chapter,verse,analysis=line.split(“”,3)ValueError:需要多个值才能执行此操作unpack@user2091683-检查您的csv文件是否真的在四个主列之间有三个空格作为分隔符,如果需要,请更改该行。但是如何知道两个列之间没有空格?
csvreader = csv.reader(csvfile, delimiter=...)
columns = ["juza","chapter","verse","analysis"]
with codecs.open("yom.csv", "rb","utf8") as f:
    for line in f:
        a, b, c, rest = line.split('   ', 3)
        doc = {k:v.strip() for k,v in zip(columns, rest.split(':'))}
        # a,b,c are the first three integers
        # doc is a dictionary