Ι'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...
Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode( utf-8 ) and decode( utf-8 ) functions accordingly in your code.
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).
Try put following code in the beginning
It's fixed my problem perfectly
import sys
reload(sys)
sys.setdefaultencoding('utf8')
teext = self.tableWidget.item(row, col).text().decode('utf-8')
Replace 'utf-8' with encoding of your text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With