# python 中文编码问题

python 中文编码问题

首先第一行肯定要写:

/# -- coding: UTF-8 --

或者

/#coding:uft8

然后在输出的汉字字符前加个u

printf u'你好'


遇到的问题:

在做数据库调试报错的时候,存在以下代码:

    try:
        tr_money.transfer(sourse_acctid,target_acctid,money)
    except Exception as e:
        print u'出现问题 :' +str(e)

其中e 为raise返回的报错字符

然后执行会报错:

'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

原因大概是对utf-8的字段不能够str

解决方法:

    try:
        tr_money.transfer(sourse_acctid,target_acctid,money)
    except Exception as e:
        print u'出现问题 :%s' %e

猜你喜欢

转载自www.cnblogs.com/Huzr/p/9078799.html
今日推荐