473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

convert utf-8 to latin-1?

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
4 6104
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
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
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
Martin v. Löwis <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Kurz | last post by:
Hello, could someone tell me the best way to convert a string to utf, for example to utf-8? The problem: I have no controll about the source where the string comes from. It could have any...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
2
by: Du Dang | last post by:
I tried to convert a block of text from iso-8859-1 to utf-8 but all I got after the convertion is gibberish. =============================== FileStream fs = File.Open("text.txt",...
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
4
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
3
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what...
3
by: Jared Wiltshire | last post by:
I'm trying to convert a wstring (actually a BSTR) to UTF-8. This is what I've currently got: size_t arraySize; setlocale(LC_CTYPE,"C-UTF-8"); arraySize = wcstombs(NULL, wstr, 0); char...
3
by: shapper | last post by:
Hello, Could someone tell me how to convert a XML file into another XML file using a XSL file with a parameter? I created the code to do it, and it seems ok, but it is not working. Could...
0
by: Zmische | last post by:
Platform: IIS6, windows 2000 Server, ASP + VBSCRIPT I have an ASP application that posts FORM via XMLHttprequest (javascript) so that all data is posted in UTF-8. So I have an ASP that get data in...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.