Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.x-privat.org!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '#if': 0.05; 'encoded': 0.05; 'error:': 0.05; 'schema': 0.05; "'')": 0.07; 'encoding:': 0.07; 'utf-8': 0.07; '"my': 0.09; '(0,': 0.09; '(1,': 0.09; 'codecs': 0.09; 'encode': 0.09; 'lines:': 0.09; 'os.path': 0.09; 'skip:[ 30': 0.09; 'subject:error': 0.11; 'subject:python': 0.11; 'index': 0.13; 'encoding': 0.15; 'basestring):': 0.16; 'codec': 0.16; 'columns': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'help?': 0.16; 'marker': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'row': 0.16; 'writer': 0.16; 'string': 0.17; 'wrote:': 0.17; 'unicode': 0.17; 'yield': 0.17; 'skip:" 40': 0.20; 'skip:v 30': 0.20; 'import': 0.21; 'doc': 0.22; 'file:': 0.22; 'txt': 0.22; 'work.': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; '(most': 0.27; 'skip:" 50': 0.27; "doesn't": 0.28; 'decimal': 0.29; 'dictionary': 0.29; 'character': 0.29; 'query': 0.30; 'code': 0.31; 'file': 0.32; 'received:84': 0.32; 'skip:s 30': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; "can't": 0.34; 'text': 0.34; 'list': 0.35; 'skip:f 40': 0.35; 'open': 0.35; 'but': 0.36; 'method': 0.36; 'drop': 0.37; 'skip:v 20': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'results': 0.65; 'header:Reply-To:1': 0.68; 'present.': 0.71; 'reply-to:no real name:2**0': 0.72; 'fields,': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=_vX2mLoUpDQA:10 a=UE9rRtIX8JoA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=xiFpfWGMdvwA:10 a=Pv3R0w2U_RPbRG4UvIwA:9 a=QEXdDO2ut3YA:10 a=Z3qxB9fdlIAXCHRL:21 a=YP0dzLOkM7oYWQtx:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 22 Feb 2013 17:35:15 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: encoding error in python 27 References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361554514 news.xs4all.nl 6980 [2001:888:2000:d::a6]:48179 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39596 On 2013-02-22 14:55, Hala Gamal wrote: > my code works well with english file but when i use text file encodede"utf-8" "my file contain some arabic letters" it doesn't work. > my code: > # 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"] > > schema = fields.Schema(juza=fields.NUMERIC(stored=True), > chapter=fields.NUMERIC(stored=True), > verse=fields.NUMERIC(stored=True), > voc=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("tt.txt",encoding='utf-8') 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("voc", ix.schema).parse(u"كتاب") > results = searcher.search(query) > print(len(results)) > print(results[1]) > my error: > Traceback (most recent call last): > File "D:\Python27\yarab (4).py", line 45, in > 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 character u'\ufeff' in position 0: invalid decimal Unicode string > **my file: > 2 2 3 كتاب > 2 2 1 لعبة > 1 1 1 كتاب > **any help? > I see that you're using Microsoft Windows. Microsoft likes to indicate that a text file contains UTF-8 by starting the text with u"\xFEFF" encoded as UTF-8. You're opening the file with the encoding "utf-8", so you're seeing that marker. Try opening the file with the encoding "utf-8-sig". That will drop the marker if it's present.