解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误

5 views
Skip to first unread message

chengjun wang

unread,
Jan 20, 2013, 10:17:00 PM1/20/13
to computational...@googlegroups.com

这两天遇到一个encoding的问题,虽然在script里设置:
# -*- coding: utf-8 -*-

但python实际encode的时候还是用ascii,于是就开始抛错。

解决的方法是使用sys来设置:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


Best regards.                         
                                       
Chengjun Wang

Web Mining Lab
Department of Media and Communication
City University of Hong Kong.
Room 5008, 18 Tat Hong AvenueRun Run Shaw Creative Media Centre
Kowloon. Hong Kong


Reply all
Reply to author
Forward
0 new messages