【发布时间】:2011-11-13 19:45:45
【问题描述】:
我有一个运行良好的 python 程序。它连接到多个网站并输出所需的信息。由于并非所有网站都使用 utf-8 编码,因此我从标头请求字符集并使用unicode(string, encoding) 方法进行解码(我不确定它是否适合这样做,但效果很好)。当我运行 python 程序时,我没有收到 ???标记,它工作正常。但是当我使用 php 的 system 函数运行程序时,我收到了这个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0131' in position 41: ordinal not in range(128)
这是一个特定于 python 的错误,但让我感到困惑的是,当我使用终端运行程序时,我没有收到此错误。只有当我使用 php 的 system 函数并从 php 调用程序时,我才会收到此消息。这个问题背后的原因可能是什么?
这是一个示例代码:
调用python程序的php代码:
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
【问题讨论】:
-
可能是环境变量。
标签: php python encoding system decoding