Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unicode:Python3在Ubuntu上作为CGI运行时忽略LC_CTYPE_Python_Python 3.x_Ubuntu_Unicode_Locale - Fatal编程技术网

Unicode:Python3在Ubuntu上作为CGI运行时忽略LC_CTYPE

Unicode:Python3在Ubuntu上作为CGI运行时忽略LC_CTYPE,python,python-3.x,ubuntu,unicode,locale,Python,Python 3.x,Ubuntu,Unicode,Locale,我是python新手,最近遇到了这个问题w。我的脚本处理的文件名中的Unicode字符。Python似乎忽略了操作系统的语言环境设置,并且没有使用utf-8编码 系统为Ubuntu 16.04,从以下“语言环境”输出: LC_ALL和LC_CTYPE似乎被正确设置为UTF-8编码。文件系统上的文件可以完美地使用Unicode字符创建 但是,如果我运行一个python脚本,它试图操作一个文件w。文件名中的Unicode字符,失败如下 代码: 但是sys.getfilesystemencoding总

我是python新手,最近遇到了这个问题w。我的脚本处理的文件名中的Unicode字符。Python似乎忽略了操作系统的语言环境设置,并且没有使用utf-8编码

系统为Ubuntu 16.04,从以下“语言环境”输出:

LC_ALL和LC_CTYPE似乎被正确设置为UTF-8编码。文件系统上的文件可以完美地使用Unicode字符创建

但是,如果我运行一个python脚本,它试图操作一个文件w。文件名中的Unicode字符,失败如下

代码:

但是sys.getfilesystemencoding总是返回“ASCII”,这似乎是问题的核心

如果我尝试在同一用户下的控制台中执行相同操作,则会发现utf-8:

strnad@apczbrno46:~$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getfilesystemencoding()
'utf-8'
>>>
你能给我指一下正确的方向吗

谢谢,

马丁

显然,我应该再搜索一段时间:)

这就是解决方案:

Summarizing @cercatrova 's answer:

Add PassEnv LANG line to the end of your /etc/apache2/apache2.conf or .htaccess.
Uncomment . /etc/default/locale line in /etc/apache2/envvars.
Make sure line similar to LANG="en_US.UTF-8" is present in /etc/default/locale.
[Thu Jul 12 14:10:27.145862 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: Error in sys.excepthook:: /var/www/html/test.py
[Thu Jul 12 14:10:27.146065 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: Traceback (most recent call last):: /var/www/html/test.py
[Thu Jul 12 14:10:27.146226 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:   File "/usr/lib/python3.5/cgitb.py", line 268, in __call__: /var/www/html/test.py
[Thu Jul 12 14:10:27.146333 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:     self.handle((etype, evalue, etb)): /var/www/html/test.py
[Thu Jul 12 14:10:27.146488 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:   File "/usr/lib/python3.5/cgitb.py", line 288, in handle: /var/www/html/test.py
[Thu Jul 12 14:10:27.146578 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:     self.file.write(doc + '\\n'): /var/www/html/test.py
[Thu Jul 12 14:10:27.146855 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: UnicodeEncodeError: 'ascii' codec can't encode character '\\xed' in position 1724: ordinal not in range(128): /var/www/html/test.py
[Thu Jul 12 14:10:27.146889 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: : /var/www/html/test.py
[Thu Jul 12 14:10:27.146960 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: Original exception was:: /var/www/html/test.py
[Thu Jul 12 14:10:27.147057 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: Traceback (most recent call last):: /var/www/html/test.py
[Thu Jul 12 14:10:27.147199 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:   File "/var/www/html/test.py", line 28, in <module>: /var/www/html/test.py
[Thu Jul 12 14:10:27.147269 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215:     os.remove(filename): /var/www/html/test.py
[Thu Jul 12 14:10:27.147540 2018] [cgi:error] [pid 1346] [client 10.0.8.37:63831] AH01215: UnicodeEncodeError: 'ascii' codec can't encode character '\\xed' in position 7: ordinal not in range(128): /var/www/html/test.py
# sys.getfilesystemencoding = lambda: 'UTF-8'
#import locale
# locale.setlocale(locale.LC_ALL, 'en_US.utf-8')
# os.environ["PYTHONIOENCODING"] = "utf-8"
# locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
# sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
# sys.stdout = io.open(sys.stdout.fileno(), 'w', encoding='utf8')
strnad@apczbrno46:~$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getfilesystemencoding()
'utf-8'
>>>
Summarizing @cercatrova 's answer:

Add PassEnv LANG line to the end of your /etc/apache2/apache2.conf or .htaccess.
Uncomment . /etc/default/locale line in /etc/apache2/envvars.
Make sure line similar to LANG="en_US.UTF-8" is present in /etc/default/locale.