0

I'm having trouble formating the strings to utf-8 In this script im getting data from excel file then printing it out in a loop, the problem is that the string with special characters shows up wrong.

In result I keep getting 'PatrÄ«cija' instead of 'Patrīcija' Can't seem to find the solution for this problem

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import sys
    import xlrd
    import datetime

    def todaysnames():
    todaysdate = datetime.datetime.strftime(datetime.date.today(), "%d.%m")

    book = xlrd.open_workbook("vardadienas.xls")
    sheet = book.sheet_by_name('Calendar')
    for rownr in range(sheet.nrows):
        if sheet.cell(rownr, 0).value == todaysdate:
            string = (sheet.cell(rownr, 1).value)
            string = string.encode(encoding="UTF-8",errors="strict")
            names = string.split(', ')
            return names

    names = todaysnames()
    for name in names:
        print name
CatWithGlasses
  • 1,493
  • 2
  • 18
  • 21

2 Answers2

1

Changed encoding to iso8859_13(Baltic languages) and it fixed it.

talha2k
  • 24,937
  • 4
  • 62
  • 81
CatWithGlasses
  • 1,493
  • 2
  • 18
  • 21
0

I think that your problem may be caused by the print. The xlrd returns utf8. Depending of the encoding of your console, the print may have difficulties to print it correctly. I've noticed this sometimes on a french Windows (where encoding is cp1252)

The following question: Python, Unicode, and the Windows console explains how to print unicode char on the console on Windows. I didn't try myself but it looks good.

I hope it helps

Community
  • 1
  • 1
luc
  • 41,928
  • 25
  • 127
  • 172
  • Judging from the solution @Ever arrived at, I suspect your answer was on the right track. (But you could explain the problem a little-- the link is useless to the uninitiated.) – alexis Aug 25 '12 at 20:12
  • Ok I've tried to explained a bit more :) . I didn't have a lot of time and just wanted to point @Ever on a possible cause of the problem. But as usual, it worths taking the required time on the 1st time! ;-) – luc Aug 25 '12 at 20:38