4


I do not managed to simply print a QString variable containing a special character.
I always get a UnicodeEncodeError:

'ascii' codec can't encode characters in position ....

Here is the code I tried without success :

var1 = "éé" #idem with u"éé"  
 var2 = QString (var1)  
 print var2  
 --->>> UnicodeEncodeError  
 print str(var2)  
 --->>> UnicodeEncoreError  
 var3 = QString.fromLocal8Bit (var1) #idem with fromLatin1 and fromUtf8  
 print var3  
 --->>> UnicodeEncodeError  

 codec = QTextCodec.codecForName ("UTF-8") #idem with ISO 8859-1  
 var4 = codec.toUnicode (var2.toUtf8().data()) #idem with toLatin1 instead of toUtf8  
 print var4  
 --->>> UnicodeEncodeError  

I also tried to use :

 QTextCodec.setCodecForCStrings(QTextCodec.codecForName("UTF-8"))  

I really need to print a QString variable, not a QByteArray or other object.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
oaimac
  • 784
  • 2
  • 12
  • 27
  • Where are you printing to? If you're e.g. using an ascii-only terminal, you can't display non-ascii characters - this has nothing to do with `QString`. Can you `print var1`? –  Jan 07 '11 at 14:08
  • Yes I can print var1. I just try to print inside a cmd prompt running the python interpretor. I also tried to print inside the Eclipse environment console with the same result. I tried with Python 2.5 and python 2.7 on Windows and Linux. – oaimac Jan 07 '11 at 14:45
  • Even the conversion str(var2) give the same error (only when var1 and then var2 contain special characters) – oaimac Jan 07 '11 at 14:47

2 Answers2

5

It works for me using toUtf8():

>>> s = u'éé'
>>> qs = QString(s)
>>> qs
PyQt4.QtCore.QString(u'\xe9\xe9')
>>> print qs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print qs.toUtf8()
éé
>>>

Your internal data should be Unicode, so you should be using u'éé' rather than just 'éé' as you stated in your in your question. Your comment even says u'éé'.

Update: Sorry, but printing or str() on Unicode cannot be guaranteed to work on Unicode objects unless you use a specific encoding. Print streams accept byte arrays/bytestrings, and str() on a Unicode object is effectively trying to convert Unicode to a byte array/bytestring. You're not going to be able to avoid byte arrays!

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • qs.toUtf8() return a QByteArray which is not what I need. I really need to just print a QString containing special characters or convert it with str(). Nevertheless, thank you for your answer – oaimac Jan 10 '11 at 08:00
1

try following:

  1. add # -*- coding: utf-8 -*- magic comment at the begging of your script (details here)
  2. use "u" string declaration with your string constant

below is an example which works for me

# -*- coding: utf-8 -*-

from PyQt4 import QtCore

var1 = u"éé" #idem with u"éé"  
print var1  

var2 = QtCore.QString(var1)
print var2

var3 = QtCore.QString(u"éé")
print var3

returns:

éé

éé

éé

hope this helps, regards

serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
  • I need passing through a QString. All the other methods, using unicode, direct convertion, different coding... seem to work but directly using a QString not. No way to just do str(QString variable) when QString variable contain special characters... – oaimac Jan 10 '11 at 08:03
  • I've changed my example a bit, see if it helps you – serge_gubenko Jan 11 '11 at 02:46
  • it does not work for me with python2.7. I have an error saying that : SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xe9 in position 0: unexpected end of data on the line var1 = u"éé" – oaimac Jan 13 '11 at 08:19