convert utf-8 to latin-1?

U

Uwe Mayer

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
 
J

Jarek Zgoda

Uwe Mayer said:
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.
 
M

Markus Faltin

Uwe said:
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:
Markus
 
G

Guest

Uwe said:
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
 
J

Jarek Zgoda

Martin v. Löwis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top