Reputation: 2761
I have a situation that has me puzzled. I have a QLineEdit in my interface so when I populate it with the following text 𐡀𐡌𐡓 𐡊𐡓' 𐡓𐡁 𐡀𐡁𐡓𐡄𐡌 𐡏𐡋 (it's aramaic) and I have the correct fonts installed on my machine so I can see the fonts in browsers and all but these fonts I had to install then myself so you might see some weird chars.
self.editor = QtGui.QLineEdit(self)
self.editor.setText(𐡀𐡌𐡓 𐡊𐡓' 𐡓𐡁 𐡀𐡁𐡓𐡄𐡌 𐡏𐡋)
so in the editor field in the ui I can see the text correctly but when I try to re-extract that text I get something weird:
editor_text = self.editor.text()
here I get an exception:
print(editor_text)
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud802' in position 0:
surrogates not allowed
pprint(editor_text)
"\ud802\udc40\ud802\udc4c\ud802\udc53 \ud802\udc4a\ud802\udc53' "
'\ud802\udc53\ud802\udc41 '
'\ud802\udc40\ud802\udc41\ud802\udc53\ud802\udc44\ud802\udc4c '
'\ud802\udc4f\ud802\udc4b'
I tried doing:
editor_text = self.editor.text().encode(encoding='utf-8', errors='surrogateescape')
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud802' in position 0:
surrogates not allowed
What could I do to fix this, thanks
EDIT:
I have of course added the header
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Upvotes: 2
Views: 3076
Reputation: 3460
Add this to the source header:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
You can read this thread: PEP Index
In your aramaic character string, add 'u' to the above Python string to use in unicode mode like this:
u' ܐܪܡܝܐ '
or
u'Unicode String'
Completed code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
class exampleQMainWindow (QtGui.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
testQLineEdit = QtGui.QLineEdit(self)
testQLineEdit.setText(u'ܐܪܡܝܐ')
print testQLineEdit.text()
self.setCentralWidget(testQLineEdit)
app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
You can get variables from somewhere else like this:
text = u'ܐܪܡܝܐ'
testQLineEdit = QtGui.QLineEdit(self)
testQLineEdit.setText(text)
print testQLineEdit.text()
To force the string to Unicode, you can use unicode(String)
.
This is the test type of output to return text:
>> print type(unicode(testQLineEdit.text()))
<type 'unicode'>
>> print type(testQLineEdit.text())
<class 'PyQt4.QtCore.QString'>
>> print type(testQLineEdit.text().toUtf8())
<class 'PyQt4.QtCore.QByteArray'>
All conditions can print in the console.
Upvotes: 2