php系统,python和utf-8 [英] php system, python and utf-8

查看:62
本文介绍了php系统,python和utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的python程序。它连接到多个网站并输出所需的信息。由于并非所有网站都使用utf-8进行编码,因此我从标头中请求字符集,并使用 unicode(string,encoding)方法进行解码(我不确定是否适当的方法来执行此操作,但效果很好)。当我运行python程序时,我没有收到???标记,并且效果很好。但是,当我使用php的 system 函数运行程序时,会收到此错误:

I have a python program running very well. It connects to several websites and outputs the desired information. Since not all websites are encoded with utf-8, I am requesting the charset from the headers and using unicode(string, encoding) method to decode (I am not sure whether its the appropriate way to do this but it works pretty well). When I run the python program I receive no ??? marks and it works fine. But when I run the program using php's system function, I receive this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0131' in position 41: ordinal not in range(128)

这是python特定的错误,但令我困惑的是,使用终端运行程序时没有收到此错误。我只有在使用php的 system 函数并从php调用该程序时才收到此消息。

This is a python specific error but what confuses me is that I don't receive this error when I run the program using the terminal. I only receive this when I use php's system function and call the program from php. What may be the cause behind this problem?

以下是示例代码:

调用python的php代码程序:

php code that calls python program:

system("python somefile.py $search") // where $search is the variable coming from an input

python代码:

encoding = "iso-8859-9"
l = "some string here with latin characters"
print unicode("<div class='line'>%s</div>" % l, encoding)
# when I run this code from terminal it works perfect and I receive no ??? marks
# when I run this code from php, I receive the error above


推荐答案

PrintFails Wiki


当Python找到连接到终端的输出时,将设置
sys.stdout.encoding 属性终端的编码。 print
语句的处理程序将自动将unicode参数编码为
str输出。

When Python finds its output attached to a terminal, it sets the sys.stdout.encoding attribute to the terminal's encoding. The print statement's handler will automatically encode unicode arguments into str output.

这就是程序正常运行的原因

This is why your program works when called from the terminal.


当Python未检测到
输出所需的字符集时,它将设置sys.stdout .encoding为None,然后print将调用
ascii编解码器。

When Python does not detect the desired character set of the output, it sets sys.stdout.encoding to None, and print will invoke the "ascii" codec.

这就是为什么程序在调用时失败的原因从PHP。
要使其在从php调用时起作用,您需要明确说明 print 应该使用的编码。例如,要明确表明您希望将输出编码为 utf-8 (未连接到终端时):

This is why your program fails when called from php. To make it work when called from php, you need to make explicit what encoding print should use. For example, to make explicit that you want the output encoded in utf-8 (when not attached to a terminal):

ENCODING = sys.stdout.encoding if sys.stdout.encoding else 'utf-8'
print unicode("<div class='line'>%s</div>" % l, encoding).encode(ENCODING)






或者,您可以设置 PYTHONIOENCODING环境变量
然后您的代码应该可以正常工作(无论是从终端还是从php调用)。


Alternatively, you could set the PYTHONIOENCODING environment variable. Then your code should work without changes (both from the terminal and when called from php).

这篇关于php系统,python和utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆