Giuseppe

Python is using your console code page (== character encoding) and
tries to convert your Unicode string into a byte sequence that
represents the string in that code page. The problem is code page 850
doesn't contain the Cyrillic characters needed
(http://en.wikipedia.org/wiki/Code_page_850) and python therefore
fails.

You could change your console code page to 65001 (run 'chcp 65001' in
console window). 65001 is equivalent to UTF8 and therefore contains
mappings for all (most) Unicode strings you could pick up as file name
from NTFS

Please note, just because python can convert your Unicode strings to a
byte sequence that is accepted by your console window doesn't mean
your system has a display font containing the data necessary to render
a character visually to represent the original Unicode code point in
the string. Looked from another angle: just because you can't render
something in a console window doesn't mean python doesn't have the
correct string internally which you could use any way you want as long
as you don't want to output it to stdout.

Another comment: (I believe) the 'dir' command will switch encoding
between bits of strings to display a directory listing made up of a
combination of non-Western European languages. Feel free to create
test files and rename them such that they name contains Arabic or
Asian strings - go to foreign language web site and copy&paste a few
funny looking characters. Windows Explorer can handle that but 'dir'
may eventually give up. So will python if you don't stick to Unicode.
Mixing file names like that is a nice test to check if a system is
handling character encodings and associated font rendering issues
correctly.

Cheers
Christian

On Thu, Jan 24, 2013 at 6:26 PM, Giuseppe Penone <[email protected]> wrote:
> I modified the rows:
>
>         filepath = chooser.get_filename()
>         chooser.destroy()
>         return filepath
>
> to:
>
>         filepath = chooser.get_filename()
>         chooser.destroy()
>         return unicode(filepath) if filepath != None else None
>
> But now I get error when opening E:\SOFTWARE\Кириллица.ctb :
>
> Traceback (most recent call last):
>   File "test.py", line 35, in <module>
>     print filepath
>   File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
>     return codecs.charmap_encode(input,errors,encoding_map)
> UnicodeEncodeError: 'charmap' codec can't encode characters in position
> 12-20: character maps to <undefined>
> Premere un tasto per continuare . . .
>
> I really don't know what to do :(
>
>
> On Thu, Jan 24, 2013 at 12:38 PM, Timo <[email protected]> wrote:
>>
>> Op 23-01-13 18:52, Giuseppe Penone schreef:
>>>
>>> Hi,
>>>
>>> while I have a file named *E:\SOFTWARE\Кириллица.ctb* on my pc, selecting
>>> it with a gtk.FileChooserDialog
>>> and printing to terminal returns weird characters
>>> *E:\SOFTWARE\ÐsиÑ?иллиÑ┼а.ctb*.
>>
>> Ah the joy of encodings on Windows. This is the piece of code I use
>> (trimmed):
>>
>> class _FileChooser(gtk.FileChooser):
>> def get_filename(self):
>> filename = gtk.FileChooser.get_filename(self)
>> if filename is not None:
>> return unicode(filename)
>>
>> def get_current_folder(self):
>> return unicode(gtk.FileChooser.get_current_folder(self))
>>
>>
>> And then subclass the above when creating a FileChooserDialog or
>> FileChooserButton, like so:
>>
>> class MyFileChooserDialog(gtk.FileChooserDialog, _FileChooser):
>> def __init__(self):
>> super(MyFileChooserDialog, self).__init__()
>>
>> class MyFileChooserButton(gtk.FileChooserButton, _FileChooser):
>> def __init__(self):
>> super(MyFileChooserButton, self).__init__("")
>>
>>
>> You can do fun things this way, but the most important thing is the
>> unicode conversion ofcourse.
>>
>> Timo
>>
>>>
>>> Does anybody have a clue of the reason?
>>> PS I have all my files starting with *# -*- coding: UTF-8 -*-*
>>>
>>>
>>> Regards,
>>> Giuseppe.
>>>
>>>
>>> _______________________________________________
>>> pygtk mailing list   [email protected]
>>> http://www.daa.com.au/mailman/listinfo/pygtk
>>> Read the PyGTK FAQ: http://faq.pygtk.org/
>>
>>
>> _______________________________________________
>> pygtk mailing list   [email protected]
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>
>
>
> _______________________________________________
> pygtk mailing list   [email protected]
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to