0 Replies - 1427 Views - Last Post: 23 February 2013 - 10:01 PM Rate Topic: -----

#1 enghalagamal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-February 13

Encoding problem

Posted 23 February 2013 - 10:01 PM

i wrote a code for indexing using whoosh in pyhon27 "i have big text file that have some arabic letters so i saved the file in utf-8" when i test the script on small file it works well but when i use the big file i have an error
# encoding: utf-8
from whoosh import fields, index
import os.path
import re,string
import codecs
from whoosh.qparser import QueryParser

# This list associates a name with each position in a row
columns = ["juza","chapter","verse","voc","unvoc"]

schema = fields.Schema(juza=fields.NUMERIC(stored=True),
                       chapter=fields.NUMERIC(stored=True),
                       verse=fields.NUMERIC(stored=True),
                       voc=fields.TEXT(stored=True),
                       unvoc=fields.TEXT(stored=True))

# 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:
  with codecs.open("last.txt",encoding='utf-8-sig') as txtfile:
    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]
          u+=1
          #if isinstance(w, basestring):
               #w = unicode(w)
          doc[fieldname] = w
      # Pass the dictionary to the add_document method
      writer.add_document(**doc)
with ix.searcher() as searcher:
    query = QueryParser("unvoc", ix.schema).parse(u"كتاب")
    print(len(results))
    print(results[0])

** the Error:
Traceback (most recent call last):
File "D:\Python27\yarab (4).py", line 46, in <module>
writer.add_document(**doc)
File "build\bdist.win32\egg\whoosh\filedb\filewriting.py", line 369, in add_document
items = field.index(value)
File "build\bdist.win32\egg\whoosh\fields.py", line 466, in index
return [(txt, 1, 1.0, '') for txt in self._tiers(num)]
File "build\bdist.win32\egg\whoosh\fields.py", line 454, in _tiers
yield self.to_text(num, shift=shift)
File "build\bdist.win32\egg\whoosh\fields.py", line 487, in to_text
return self._to_text(self.prepare_number(x), shift=shift,
File "build\bdist.win32\egg\whoosh\fields.py", line 476, in prepare_number
x = self.type(x)
UnicodeEncodeError: 'decimal' codec can't encode characters in position 0-4: invalid decimal Unicode string

**my text file sample:
14 4 4 زهرة زهر
114 5 3 فِى في
114 5 4 صُدُورِ صدور
114 5 5 الناس الناس
114 6 1 من من
114 6 2 كتاب كتب

Is This A Good Question/Topic? 0
  • +

Page 1 of 1