2

I'm trying to make indexing to an excel file and i used whoosh package but, i found an error that the list index is out of range. please, can anyone help me? my code is:

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()
`

and i get this error and i dont know why? error:

Traceback (most recent call last):
  File "C:\Python27\yarab.py", line 39, in <module>
    fieldname = columns[colnum]
IndexError: list index out of range

and my csv file :

1   3   1   Al+ POS:ADJ LEM:r~aHoma`n ROOT:rHm MS GEN
1   3   2   Al+ POS:ADJ LEM:r~aHiym ROOT:rHm MS GEN
1   4   1   POS:N ACT PCPL LEM:ma`lik ROOT:mlk M GEN
1   4   2   POS:N LEM:yawom ROOT:ywm M GEN
1   4   3   Al+ POS:N LEM:diyn ROOT:dyn M GEN
1   5   1   POS:PRON LEM:&lt;iy~aA 2MS
Mp0int
  • 18,172
  • 15
  • 83
  • 114

1 Answers1

0

csv.reader uses as default delimiter comma: ,

You have to define your delimiter explicitely:

csvreader = csv.reader(csvfile, delimiter=...)

However, your CSV file is not homogenous. It will be better to read it without 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
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • Do you mean that i should delete the "csvreader" and replace it with your suggested code? but if i do this,the problem now is that how can i bring the field name in the following line : "fieldname = columns[colnum]" – user2091683 Feb 20 '13 at 15:26
  • @user2091683 - you don't need `colnum` any more. `zip(columns, rest.split(':'))` "zips" them together and `doc`-dictionary contains the whole entry. – eumiro Feb 20 '13 at 15:29
  • This is my new code,Kindly open this link : (http://pastebin.com/qWPZsiyd) This error occurs: Traceback (most recent call last): File "C:\Python27\yarab.py", line 26, in juza , chapter, verse , analysis = line.split(' ', 3) ValueError: need more than 1 value to unpack – user2091683 Feb 20 '13 at 15:42
  • @user2091683 - check whether your csv-file really has three spaces as delimiter between the four main columns and change that line if needed. – eumiro Feb 20 '13 at 15:47
  • but how to know no of spaces between the coumns? – user2091683 Feb 20 '13 at 15:49
  • @user2091683 - if you cannot check in your CSV-file (just by looking into it), then you can also have a look at regular expressions `re.split`, which will allow you to split using variable strings. – eumiro Feb 20 '13 at 15:57
  • i have replaced iit with(' ',3) but i got this big error:(http://pastebin.com/mhbydsTr) – user2091683 Feb 20 '13 at 16:07