Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98979

handling of non-ASCII filenames?

From Ulli Horlacher <framstag@rus.uni-stuttgart.de>
Newsgroups comp.lang.python
Subject handling of non-ASCII filenames?
Date 2015-11-18 16:45 +0000
Organization University of Stuttgart, FRG
Message-ID <n2i9vt$icf$1@news2.informatik.uni-stuttgart.de> (permalink)

Show all headers | View raw


I have written a program (Python 2.7) which reads a filename via
tkFileDialog.askopenfilename() (was a good hint here, other thread).

This filename may contain non-ASCII characters (German Umlauts).

In this case my program crashes with:

  File "S:\python\fexit.py", line 1177, in url_encode
      u += '%' + c.encode("hex").upper()
  File "C:\Python27\lib\encodings\hex_codec.py", line 24, in hex_encode
    output = binascii.b2a_hex(input)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 0: ordinal not in range(128)


This is my encoding function:

def url_encode(s):
  u = ''
  for c in list(s):
    if match(r'[_=:,;<>()+.\w\-]',c): 
      u += c
    else:
      u += '%' + c.encode("hex").upper()
  return u



As I am Python newbie I have not quite understood the Python character
encoding scheme :-}

Where can I find a good introduction of this topic?

I would also appreciate a concrete solution for my problem :-)

-- 
Ullrich Horlacher              Server und Virtualisierung
Rechenzentrum IZUS/TIK         E-Mail: horlacher@tik.uni-stuttgart.de
Universitaet Stuttgart         Tel:    ++49-711-68565868
Allmandring 30a                Fax:    ++49-711-682357
70550 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

handling of non-ASCII filenames? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 16:45 +0000
  Re: handling of non-ASCII filenames? Chris Angelico <rosuav@gmail.com> - 2015-11-19 03:54 +1100
    Re: handling of non-ASCII filenames? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 18:09 +0000
      Re: handling of non-ASCII filenames? Chris Angelico <rosuav@gmail.com> - 2015-11-19 07:29 +1100
        Re: handling of non-ASCII filenames? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 22:37 +0000
  Re: handling of non-ASCII filenames? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-18 18:22 +0100
    Re: handling of non-ASCII filenames? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-19 07:54 +0000
      Re: handling of non-ASCII filenames? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-19 09:16 +0100

csiph-web