python编码和解码字符串

时间:2011-08-07 01:23:22

标签: python string

我正在从文件中读取Python中的名字和姓氏,但我遇到编码问题。我想要打印出这个名字,但是得到“UnicodeEncodeError:'charmap'编解码器无法编码位置11中的字符'\ u021b':字符映射到未定义”使用此代码:

    first = obj.first_name.encode('utf-8')
    last = obj.last_name.encode('utf-8')
    first = first.decode('utf-8')
    last = last.decode('utf-8')
    print(first)

有什么问题?

1 个答案:

答案 0 :(得分:2)

问题是您的输出终端/控制台无法显示ț。如果您使用的是Windows,请在运行脚本之前尝试chcp 65001