0

I have a text file with accentuated letters. I set my python file utf-8 encoding, but after reading given cells, the accentuated letters doesn't show properly.

for file in glob.glob('N:\Egyadat\*.xls'):
    book = xlrd.open_workbook(file, formatting_info=True, encoding_override="utf-8")
    sheets = book.sheet_names()
    first_sheet = book.sheet_by_index(1)
    print first_sheet.name
    rows, cols = first_sheet.nrows, first_sheet.ncols
    print "Number of rows: %s   Number of cols: %s" % (rows, cols)
    value = first_sheet.cell(7, 0)
    print value

The code gives 'H\xedd neve:' but the correct would be 'Híd neve'

krszt
  • 63
  • 1
  • 7
  • The encoding used to open the file must be the same as the encoding used to create the file. If you're not sure, you have to ask whoever created the file to tell you the correct encoding, or must must guess the encoding by try-and-error. – Mike Scotty Jul 24 '19 at 08:27
  • possible duplicate of https://stackoverflow.com/questions/27592240/open-a-text-file-with-accents-in-python – venkata krishnan Jul 24 '19 at 08:27
  • 1
    Possible duplicate of [Open a text file with accents in python](https://stackoverflow.com/questions/27592240/open-a-text-file-with-accents-in-python) – venkata krishnan Jul 24 '19 at 08:28
  • 2
    What @MikeScotty said, plus keep in mind that displaying the value also needs to be done in the same encoding, so if your command line doesn't support / is not set for the same encoding, you'll not see it properly – ChatterOne Jul 24 '19 at 08:29
  • I added an extra row to my script: print file.decode('utf-8'). Now it gives UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 53: invalid continuation byte – krszt Jul 24 '19 at 09:29

0 Answers0