I have that piece of code
this won't print the same chars (æâ€Â¢Ã°)...
If I do "print unicode(a).encode("utf-8") I won't get æâ€Â¢Ã°, instead of that I'll get exactly the same thing as the another print.
What should I do?
Printable View
I have that piece of code
this won't print the same chars (æâ€Â¢Ã°)...
If I do "print unicode(a).encode("utf-8") I won't get æâ€Â¢Ã°, instead of that I'll get exactly the same thing as the another print.
What should I do?
supposing your file encoding is utf8, use
That works great in console, however, I think that this wasn't what I was looking for.
I have a QTextEdit and I get the text with .toPlainText()
Then I have to pass that string to str() but it will fail with that error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
Any ideas? Maybe .toPlainText() return unicode and not utf-8 ?
Pass the string with QString::toUtf8().
msg = QString.fromUtf8(self.ui.inputWidget.toPlainText() )
message = str(QString.toUtf8(msg))
message.content.encode("utf-8") << This will fail with UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
It's utf8, because Qt handles all as utf by default. But of what str function are you talking about? And what type is message? But anyway maybe QString::toLocal8Bit() will help you.
Lets see...
I get the text from the QTextEdit widget this way:
msg = QString.fromUtf8(self.ui.inputWidget.toPlainText() )
So, "msg" is a QString utf8 encoded, right?
Next, I want to get only the string from that QString object, so I do
message = str(QString.toUtf8(msg))
Next, I have another method that receives that "message" string and tries to do .encode("utf-8") and there's where my app fails, because message.encode("utf-8") says that "ascii" codec can't decode byte.......
And that were I'm stuck.
Ok... lets make it even easier...
I'll just get the text this way:
msg = self.ui.inputWidget.toPlainText()
So, "msg" will be a QString encoded in utf8. How can I convert that string to python str? (without loosing data)
EDIT: I would like to have the text in a python str, so, whenever I do "str(message)" or "message.encode("utf-8")" it will work without problems.
Nah, forget my last 2 posts.
I just want to get that text from the QTextEdit and be able to do "print message.encode("utf-8")" and see exactly what I typed. Is that possible?