2

I would like to use bottle (bottlepy, bottle.py) for a dictionary application allowing the input to be specified in the URL (GET) like

http://dictionary_domain/lookup/Thanksgiving

using

@bottle.route('/lookup/<word>')
def request( word="" ):
    print(word)

The trouble is, non-ASCII characters are URL-encoded (usually, the browser takes care of this) and there seems to be a problem with bottle.py's parsing or my general character encoding settings. Examples:

..lookup/Olivenöl:

"Olivenöl"

..lookup/Öl:

Traceback (most recent call last):
    File "bottle.py", line 763, in _handle
        return route.call(**args)
    File "bottle.py", line 1572, in wrapper
        rv = callback(*a, **ka)
    File "dictionary.py", line 63, in request
        print( "bottle: \"{}\" requested".format( word ) )
    File "C:\Python32\lib\encodings\cp850.py", line 19, in encode
        return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 10: character maps to <undefined>

The characters in question encode to

capital  Ö -> %C3%96
minscule ö -> %C3%B6

The output decodes to ö -> c3b6

but I don't know how to fix the encoding-mess.. I guess that bottle.py needs to take care of this.

The script is using # -*- coding: utf-8 -*- (also the file is UTF-8 with BOM) and regular strings print to console just fine.

Software versions: bottle.py: '0.11.4' and '0.12-dev' Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32

5

1 Answer 1

0

Encode your word in US-ASCII and upon receiving in route handler decode to required charset

2
  • Hi and thanks, but a solution requires working with the standard encodings, i.e. direct user input. I'm not sure whether the browser's encoding is standard, though, but for my purpose it may be seen as such.
    – handle
    Nov 22, 2012 at 11:33
  • Sorry, I meant I did not know whether all browsers support percent-encoding nor whether this is a requirement. Please follow the link in my comment to the question for an explanation of how the encoding is handled and under which circumstances it may be converted. Thanks for trying to help, though.
    – handle
    Nov 22, 2012 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.