37,719
社区成员
发帖
与我相关
我的任务
分享
#!/usr/local/bin/python3
#coding=gbk
import io, sys, time, re, os
def main():
try:
print("a")
print("中")
print("b")
except Exception as e:
print("Exception: " + str(e))
if __name__ == '__main__':
main()
[kkk@vs ~]$ python3 /home/kkk/9/app/tw/tw3.py
a
中
b
[kkk@vs ~]$ crontab -l
*/1 * * * * /usr/local/bin/python3 /home/kkk/9/app/tw/tw3.py > cronlog5
[kkk@vs ~]$ more cronlog5
a
Exception: 'ascii' codec can't encode character '\u4e2d' in position 0: ordinal not in range(128)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
[kkk@vs ~]$ python
Python 2.4.3 (#1, Sep 21 2011, 20:06:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>>
[kkk@vs ~]$ crontab -l
*/1 * * * * export LANG="en_US.UTF-8"; /usr/local/bin/python3 /home/kkk/9/app/tw/tw3.py > cronlog5
#!/usr/local/bin/python3
#coding=gbk
import io, sys, time, re, os
import locale
def main():
try:
defEnco = sys.getdefaultencoding()
print(defEnco)
print("a")
print("中")
print("b")
except Exception as e:
print("Exception: " + str(e))
if __name__ == '__main__':
main()
[kkk@vs ~]$ more cronlog8
utf-8
x
Exception: 'ascii' codec can't encode character '\u4e2d' in position 0: ordinal not in range(128)
[kkk@vs ~]$
[kkk@vs ~]$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
[kkk@vs ~]$