2013-10-21 112 views
2

我在打印特殊字符時遇到問題。 如果我想打印出一個包含特殊字符的簡單字符串,並在瀏覽器中打開python文件,則字符串不會顯示出來。 我試圖把無法在Python中打印特殊字符(編碼,特殊字符,lighttpd)

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

頂部在每一個可能的組合 - 沒有工作。 我嘗試使用stdout和編碼我的字符串與.enc​​ode('utf8')的解決方案。 最後一個解決方案對我有幫助,但是我的字符串然後以字符串形式呈現,我需要切換JSON,所以我不想有任何東西,但是打印出特殊字符的空白JSON字符串。 是否有可能,lighttpd存在問題,不允許我以UTF-8打印?我必須先更改lighttpd.conf嗎?

大氣壓我的配置文件看起來是這樣的:

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" 
+0

可以肯定的是:當你說「在瀏覽器中打開python文件」時,你的意思是1)查看python文件的源代碼或2)查看以CGI格式執行的python腳本的輸出?如果2):你已經嘗試過在源代碼中使用文字,但是用戶提交的字符串會發生什麼(例如使用POST的簡單形式)?我們可以看到腳本的原始輸出(HTTP標頭等)嗎? – Tibo

+0

2)輸出在文件中打開(它不直接在瀏覽器中執行,我不明白)。如果我使用特殊字符,則文件中的輸出完全爲空。 print(「ätzend」) – Codehai

回答

0

終於讓我找到了解決辦法:

output = message 
json.dumps(output) 
return output 

不能工作:-)

output = message 
return json.dumps(output) 

是返回JSON的正確方法。

0

聲明與魔法評論的編碼使用Python來解讀爲此您的字符串文字。但你只是一半。 您的文件必須實際編碼爲utf-8才能生效。

使用良好的文本編輯器(例如Notepad ++),您可以控制文件在磁盤上的真實編碼方式。

請注意,您是如何檢查使用正確的編碼是雷區:您的字符串可能會經過文件存儲,然後各種表示(例如變量,數據庫等)在發送到瀏覽器之前(涉及Content-Type聲明和潛在的自動檢測)。我強烈建議您分別檢查所有這些步驟。

+0

我正在使用Linux Ubuntu的Eclipse Kepler。我使用Python 3.3.2和Lighttpd。如果我打印出像「ärgerlich」這樣的簡單字符串,它不會在Firefox瀏覽器中顯示任何內容。 – Codehai