UnicodeEncodeError 仅在作为 cron 作业运行时 [英] UnicodeEncodeError only when running as a cron job
本文介绍了UnicodeEncodeError 仅在作为 cron 作业运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的程序在命令行中正常运行,但是当我将它作为 cron 作业运行时,它会因错误而崩溃:
My program works right in the commandline, but when I run it as a cron job it crashes on the error:
UnicodeEncodeError: 'ascii' codec can't encode character
u'xa7' in position 13: ordinal not in range(128)
它在语句上崩溃
print title
为什么只有当应用作为 cron 作业运行时才会发生这种情况?这怎么解决?
Why this is happening only when the app runs as a cron job? How could this be fixed?
我尝试过(没有帮助):
I tried (with no help):
print unicode(title)
Python 是 2.7
Python is 2.7
推荐答案
旁白:这是一个常见问题;因此,这可能是一个重复的问题.
2.7 的默认编码是 ascii
.
您需要为程序的输出提供编码.
常用的编码是utf8".
The default encoding on 2.7 is ascii
.
You need to provide an encoding for your program's output.
A common encoding to use is 'utf8'.
所以你会这样做:
print title.encode('utf8')
<小时>
这是检查默认编码的一种方法:
Here's one way to check the default encoding:
import sys
sys.getdefaultencoding()
# -> 'ascii'
这篇关于UnicodeEncodeError 仅在作为 cron 作业运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文