Dear all,
I need help with unicode. After reading a lot on several webpages I still cannot solve my problem.
I have a wx window in python. In one field I set a filename, say "áio" (stupid name, but just to give you the idea).
If I want to create a text file with that name, I would add to the

self.text_ctrl_1.GetValue()

the ".txt" string... and I get "Γ΅io.txt".
How can I get the proper filename when I save it?

My script is set as

# -*- coding: UTF-8 -*-

And what if I would like to use an image like this "img_áio.jpg" as follows?

Thumb1 = wx.Image("c:\whatever\img_áio.jpg", wx.BITMAP_TYPE_JPEG)
self.bitmap_1.SetBitmap(wx.BitmapFromImage(Thumb1))

If I run the script as "python.exe myscript.py" I get an error (Failed to load image from file "whatever\img_áio.jpg".) but the window has the right image in bitmap_1.

What am I doing wrong? or how can I fix the two issues above?
Thanks a lot

Recommended Answers

All 3 Replies

Which version of python are you using. If you are with python 2.7, a good thing to do first is to import

from __future__ import unicode_literals

at the top of the file, so that literal strings are automatically unicode. If it doesn't suffice, you can also try

fsencoding = sys.getfilesystemencoding()
filename = "c:\whatever\img_áio.jpg".encode(fsencoding)

Thanks a lot!
The first solution seems to work (yes I am using 2.7, forgot to mention it).
Now I will need to get rid of all the other changes I've done in the code with encoding and decoding, but at least I guess you put me on the right way.
I just don't seem to be able to solve this very line though

apppath = os.path.join(os.environ['APPDATA'], 'myapp')

If the path has the same á character, I get an error which doesn't seem to be solved with either encoding or decoding...

If the type of os.environ['APPDATA'] is str, you may need to decode it first with the appropriate encoding

apppath = os.path.join(os.environ['APPDATA'].decode(...), 'myapp')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.