Question

I am having a problem printing out special characters. If I want to print out a simple string with special characters in it and I open the python file in my browser, the string does not show up. I tried putting

#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-

on top in every possible combination - did not work. I tried solutions like using stdout and encoding my string with .encode('utf8'). The last solution helped me, but my string was then presented in bytestring and I need to handover JSON, so I don't want to have anything but my blank JSON string with special characters printed out. Is it possible, that there is a problem with lighttpd, not allowing me to print in UTF-8? Do I have to change the lighttpd.conf first?

Atm my config-file looks like this:

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_rewrite",
)

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"

index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm",
                               " index.lighttpd.html" )

url.access-deny             = ( "~", ".inc" )

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" , ".py")

## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"

dir-listing.encoding        = "utf-8"
server.dir-listing          = "enable"

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "text/plain; charset=utf-8", "text/html; charset=utf-8", "text/css; charset=utf-8", "text/xml; charset=utf-8", "text/javascript; charset=utf-8", "text/x-js", "application/x$
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
Was it helpful?

Solution 2

finally I found the solution:

output = message
json.dumps(output)
return output

couldn't work :-)

output = message
return json.dumps(output)

is the correct way to return the json.

OTHER TIPS

The encoding you declare with the magic comment is used by Python to intepret your string literals. But you are only halfway through. Your files must be actually encoded in utf-8 for this to work.

With a good text editor (e.g Notepad++) you have a way to control how the file is really encoded on disk.

Be aware though that how you check that the proper encoding is used is a minefield: your strings may go through file storage, then all sorts of representations (e.g. Variables, database, etc.) before they are sent to the browser (which involves Content-Type declarations and potential automatic detection). I strongly advise you to check all thèse steps separately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top