By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

convert utf-8 to latin-1?

P: n/a
Hi,

in a PyQt application I'm writing, the user can select a filename with a
FileDialog. Then I would like to open this file. This works fine if there
are no "special symbols" in the filename, such as the german umlauts: "ÖÄÜ"

Currently I convert the returned, of what I'm pretty sure its a utf-8 string
object, to a normal string using the str() function. But then my string
contains garbage and I cannot find that file on my ext3 fs (which uses
latin-1).

So my first question is wether you *can* convert it without loosing
something and secondly: how?

Thanks for any pointers -
Ciao
Uwe
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Uwe Mayer <me*****@hadiko.de> pisze:
in a PyQt application I'm writing, the user can select a filename with a
FileDialog. Then I would like to open this file. This works fine if there
are no "special symbols" in the filename, such as the german umlauts: ""

Currently I convert the returned, of what I'm pretty sure its a utf-8 string
object, to a normal string using the str() function. But then my string
contains garbage and I cannot find that file on my ext3 fs (which uses
latin-1).

So my first question is wether you *can* convert it without loosing
something and secondly: how?


I use unicode() to convert QString returned by Qt widgets to unicode
object, then encode it in latin2. Qt uses its own string class, not
ordinary strings.

--
Jarek Zgoda
Unregistered Linux User #-1
http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
Jul 18 '05 #2

P: n/a
Uwe Mayer wrote:
in a PyQt application I'm writing, the user can select a filename with a
FileDialog. Then I would like to open this file. This works fine if there
are no "special symbols" in the filename, such as the german umlauts: "ÖÄÜ"

Currently I convert the returned, of what I'm pretty sure its a utf-8 string
object, to a normal string using the str() function. But then my string
contains garbage and I cannot find that file on my ext3 fs (which uses
latin-1).


Decode the string to unicode and then encode it to latin-1:
filename_latin1 = unicode(filename_utf8, 'utf-8').encode('latin-1')


Markus
Jul 18 '05 #3

P: n/a
Uwe Mayer wrote:
Currently I convert the returned, of what I'm pretty sure its a utf-8 string
object, to a normal string using the str() function.
Try converting the object to a Unicode object. I don't know what you
mean by "utf-8 string object"; Python has no such thing (but PyQt may).

Then, pass the unicode object to open().
So my first question is wether you *can* convert it without loosing
something and secondly: how?


In Python 2.3, you best use unicode strings to represent file names;
Python should know how to map them to the local representation,
depending on operating system conventions.

Regards,
Martin

Jul 18 '05 #4

P: n/a
Martin v. Lwis <ma****@v.loewis.de> pisze:
Currently I convert the returned, of what I'm pretty sure its a utf-8 string
object, to a normal string using the str() function.


Try converting the object to a Unicode object. I don't know what you
mean by "utf-8 string object"; Python has no such thing (but PyQt may).


Any "string value" is returned by PyQt as QString, which is "compatible
with UTF-8 strings", as Qt documentation states. In programming practice
this means that you can use str() only when you expect receiving pure
ASCII text. In any other case you should use unicode() to convert
QString to unicode object. This fairly means taht you should *always*
use unicode().

Unless you write software that is allowed to run only in US.

--
Jarek Zgoda
Unregistered Linux User #-1
http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.