Antoni4040
Antoni4040

Reputation: 2319

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)

Ι'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...

Upvotes: 6

Views: 26045

Answers (3)

blueman010112
blueman010112

Reputation: 476

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

import sys
reload(sys)
sys.setdefaultencoding('utf8')

Upvotes: 2

Daniil Ryzhkov
Daniil Ryzhkov

Reputation: 7596

teext = self.tableWidget.item(row, col).text().decode('utf-8')

Replace 'utf-8' with encoding of your text

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123440

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).

Upvotes: 16

Related Questions