6

Ι've tried all the solution that I could find, but nothing seems to work:

teext = str(self.tableWidget.item(row, col).text())

I'm writing in greek by the way...

Antoni4040
  • 2,297
  • 11
  • 44
  • 56

3 Answers3

16

Clearly, self.tableWidget.item().text() returns Unicode, and you need to use the decode method instead:

self.tableWidget.item(row, col).text().encode('utf8')

You really want to review the Python Unicode HOWTO to fully appreciate the difference between a unicode object and it's byte encoding.

Another excellent article is The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!), by Joel Spolsky (one of the people behind Stack Overflow).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Well, i do not get errors any more, but when I'm writing teext to an .xls file, when I open the .xls file it shows nothing... – Antoni4040 Aug 08 '12 at 13:41
  • 1
    Read the articles I included in my answer; you'll need to find out what encoding you need to use. – Martijn Pieters Aug 08 '12 at 13:43
  • teext = unicode(self.tableWidget.item(row, col).text()) seems to solve the problem... Anyway, thanks a lot for your help... – Antoni4040 Aug 09 '12 at 07:50
2

Try put following code in the beginning
It's fixed my problem perfectly

import sys
reload(sys)
sys.setdefaultencoding('utf8')
blueman010112
  • 456
  • 1
  • 7
  • 19
1
teext = self.tableWidget.item(row, col).text().decode('utf-8')

Replace 'utf-8' with encoding of your text

Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58