Active Topics

 


Reply
Thread Tools
Nathraiben's Avatar
Posts: 267 | Thanked: 408 times | Joined on May 2010 @ Austria
#1
There I am back with one of my (probably stupid) questions!

I have a problem with handling non-ascii characters in python and was wondering whether one of the Python buffs could help me out. Here's the problem:

(For my application menu folder application) I would like to offer the user a way to enter names for their folders that include non-ascii characters. Entering them in a widget works fine, but as soon as I try to work with the string (for which I think I HAVE to convert it into a QString?), things get nasty. It works fine when I get rid of the non-ascii chars beforehand, but this results in a rather crippled, non-international functionality.

Same with reading application names. As soon as my scripts run into a name that features non-ascii characters, it simply ignores the entry and jumps right to the next one.

Is there any way to deal with non-ascii characters in Python OTHER than simply getting rid of them?

Thanks in advance!
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#2
Python should handle UTF-8 natively, though converting to/from a QString can be a bit more tricky. If it's been input in UTF-8, you should be able to use QString.fromUtf8 to convert it to a QString. To do the reverse, I had to do 'unicode(strng.toUtf8(), "utf-8")'.
 

The Following User Says Thank You to Rob1n For This Useful Post:
Nathraiben's Avatar
Posts: 267 | Thanked: 408 times | Joined on May 2010 @ Austria
#3
Once again thanks, Rob1n! I'll give it a try tomorrow - it would be incredibly nice if it's simple as that (that one already gave me lots of headaches so far).
 
Posts: 56 | Thanked: 31 times | Joined on Jul 2008 @ Austria
#4
With gtk, it's seamless. It uses UTF-8 everywhere.
 
Posts: 70 | Thanked: 39 times | Joined on Mar 2010
#5
Depending on the encoding of your input, you could do something like this:

Code:
encoding = "utf-8"
decoded = encoded.decode(encoding)
qStr = QString("Decoded: %s", decoded)
 

The Following User Says Thank You to cheungs For This Useful Post:
Nathraiben's Avatar
Posts: 267 | Thanked: 408 times | Joined on May 2010 @ Austria
#6
@dannym: It's nice that you're trying to help, but bringing up a couple of rather outdated threads doesn't quite seem the way to go...

If nothing happened in a thread for more than two weeks, chances are that either the problem got solved (thanks again, Rob1n) or the thread starter has lost interest, anyway.

@cheungs: That's what I tried at first, but sadly it didn't work, since decode() simply failed on non-ascii characters. Which is rather strange, since from the description it is meant to do the conversion itself...?

Btw, the only way to get it to work was using some rather horrid toUtf8-fromUtf8 constructs like:

Code:
unicode(QString.fromUtf8(lang.gettext(filetxt)).toUtf8(),  "utf-8")
 
Reply


 
Forum Jump


All times are GMT. The time now is 23:32.