2

I have an excel sheet like below, I need to read the values from the cell and compare with another file.

        0    1   2    3   4    5
1100    ᄀ   ᄁ   ᄂ   ᄃ   ᄄ   ᄅ

1120    ᄠ   ᄡ   ᄢ   ᄣ   ᄤ   ᄥ

1140    ᅀ   ᅁ   ᅂ   ᅃ   ᅄ   ᅅ

I have written the below piece of code after googling for opening and reading the contents of the excel sheet in python

import xlrd

file_location = "C:/Users/yepMe/Desktop/try/language.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(3)
data = [[sheet.cell_value(r,c) for c in range(sheet.ncols)] for r in range(sheet.nrows)] 
print type(data)
print data[1][0]

Now this doesn't throw any error and prints 0.0 , but when I try to print any other value which tries to print the Korean characters like print data[2][2], it gives the below error:

print data[2][2]

File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u1121' in position 0: character maps to <undefined>,

Kindly tell me what needs to be done to get the Korean characters. If I do print ord(data[2][2]), it prints the unicode code point, but when I am trying to loop through it:

for char in data:
    decVal = ord(char)

Traceback (most recent call last):
  File "C:\Users\amondapr\Desktop\korean\fonts\ttfq_protima.py", line 78, in <mo
dule>
    decVal = ord(char)
TypeError: ord() expected string of length 1, but list found

It gives the above error how do I fetch the values, what am I doing wrong?

Oliver W.
  • 13,169
  • 3
  • 37
  • 50
YepMe
  • 159
  • 2
  • 11
  • What are you running this in? A Windows command prompt? – jedwards Mar 20 '15 at 09:25
  • Yes , I am running on windows command prompt – YepMe Mar 20 '15 at 09:52
  • 2
    That will be a problem, since the windows command can't doesn't easily support UTF-8 at all. If you run this script in a console that can support UTF-8 output (for example, Eclipse IDE, IDLE, etc.), and save the script as UTF-8, your code will work exactly as you wrote. – jedwards Mar 20 '15 at 10:27

0 Answers0