0

I'm using xlrd and cx_freeze.

Now when I'm trying to read from the excel file, it shows an error when it came to the "," mark:

UnicodeEncodeError: 'charmap' codec can't encode character "\u2019" in position 12: character maps to <undefined>

from xlrd3 import *
book= open_workbook('s1.xls')
sheet=book.sheet_by_index(0)
import sys
from encodings import *
from codecs import *
def pr():
    global row,col
    if isinstance((sheet.cell(row,col).value), float):
        cell = sheet.cell(row,col)
        cell_value = cell.value
        cell_value1= int(cell.value)
        s=[]
        s.append(cell_value1)
        print (s)
        s=[]
    else:
        cell = sheet.cell(row,col)
        cell_value = cell.value
        s=[]
        s.append(cell_value)
        print (s)
        s=[]

def co():
    x=input("'S'earch again or 'Q'uite?: ")
    if x == 'S' or x=='s':
        search()
    elif x == 'Q'or x == 'q':
        sys.exit(0)
    else:
        print ('Please enter a Vailed answer: ')
        co()

def search():
    global row,col
    s=[]
    a=(input("Enter Search : "))
    for row in range(sheet.nrows):
        for col in range(sheet.ncols):
            s.append(str(sheet.cell(row,col).value))
            if a in (str(sheet.cell(row,col).value)):
                for col in range(sheet.ncols):
                    pr()    
            else:
                s=[]
    co()

search() 

this is the code

John Machin
  • 81,303
  • 11
  • 141
  • 189
  • 1
    I am not sure I can help but I think anyone who is going to be able to help is going to need to see the code you wrote to read in the file. presenting the error is good but the code that was running when the exception was called is even more critical. – PyNEwbie Dec 04 '12 at 18:40
  • Thanks pynewbie for the comment , that is the code – Mina Nessem Dec 04 '12 at 18:54
  • This is essentially a duplicate of http://stackoverflow.com/questions/11050292/prevent-encoding-errors-in-python only with a lot of extraneous text and with helpful pointers, like a stack trace, removed. I think it should be flagged as a duplicate. – Andrew Dalke Dec 04 '12 at 20:02
  • This question has nothing to do with xlrd or cx-freeze. Tags removed. The problem can be reproduced (on a Windows box) in ONE line of code: `print u"\u2019"` – John Machin Dec 31 '12 at 00:42

1 Answers1

1

You don't show which line of code your error came from. A full traceback is much more informative than only showing the error message.

A UnicodeEncodeError (note: not 'UnicodeEncodingError' as you titled this thread) often shows up when you have a Unicode string and you are passing it to something which supports an unknown encoding method. The most likely situation here is in your print(s). A traceback would have told us if this was the source of the problem.

The problem is that Python doesn't know how to print non-ASCII Unicode characters to the terminal. In this case it's because you have character '\u2019', which is the right single quotation mark. (And not the "," mark, which is a comma.)

You must tell it how to encode the Unicode as a set of bytes appropriate for your terminal; specifically, a Windows terminal. This reduces your problem to the one discussed at Prevent encoding errors in Python and several dozen other posts which you get when searching for your error message here.

Since you are on Windows, take the advice from that "Prevent encoding errors in Python" link and do:

print(s.encode('cp850', errors='replace'))
Community
  • 1
  • 1
Andrew Dalke
  • 14,889
  • 4
  • 39
  • 54
  • Thank you andrew a lot it's File "C:\python\32-bit\3.2\lib\encodings\cp720.py", line 32 in encode then the error message sorry for the error and not to type the whole msg cause it's appears for a second on the exe file then it's crashed so i've took a print screen so i can read it , so if the error line made a different please tell me thanks a lot: ) – Mina Nessem Dec 04 '12 at 20:07
  • The problem is that "right single quotation mark" doesn't occur in cp720. By default it raises an error when it doesn't know what to do. You need to tell Python that it's okay to 'replace' that character with a "?" or to 'ignore' any problems. – Andrew Dalke Dec 04 '12 at 20:09