UnicodeEncodeError:"charmap"编解码器无法编码字符...问题 [英] UnicodeEncodeError: 'charmap' codec can't encode character... problems

查看:434
本文介绍了UnicodeEncodeError:"charmap"编解码器无法编码字符...问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有人问我十亿次这个问题之前,请注意,我已经尝试了多个线程中的几个答案,但是似乎都没有一个问题能正确解决我的问题.

Before anyone gives me crap about this being asked a billion times, please note that I've tried several of the answers in many a thread but none of them seemed to work properly for my problem.

import json
def parse(fn):
    results = []
    with open(fn) as f:
        json_obj = json.loads(open(fn).read())
        for r in json_obj["result"]:
            print(r["name"])

parse("wine.json")

我基本上只是打开一个json文件并对其进行迭代以获得一些值.显然,每当我读取其中包含unicode的值时,都会出现此错误.

I'm basically just opening a json file and iterating it for some values. Obviously, whenever I read a value with some unicode in it I get this error.

Traceback (most recent call last):
  File "json_test.py", line 9, in <module>
    parse("wine.json")
  File "json_test.py", line 7, in parse
    print(r["name"])
  File "C:\Python34\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u201c' in position
15: character maps to <undefined>

正如人们在其他线程中所说的那样,我尝试对它进行编码,但无论如何编码和/或解码,都会遇到类似的错误.请帮忙.

As people said in other threads I've tried to encode it and whatnot, but then I get a similar error, no matter how I encode and/or decode it. Please help.

推荐答案

一切正常,直到尝试打印字符串为止.要打印字符串,必须首先将其从纯Unicode转换为输出设备支持的字节序列.这要求将encode设置为正确的字符集,Python将该字符集标识为cp850-Windows控制台默认值.

Everything is fine up until the point where you try to print the string. To print a string it must first be converted from pure Unicode to the byte sequences supported by your output device. This requires an encode to the proper character set, which Python has identified as cp850 - the Windows Console default.

从Python 3.4开始,您可以通过在命令提示符处发出以下命令,将Windows控制台设置为使用UTF-8:

Starting with Python 3.4 you can set the Windows console to use UTF-8 with the following command issued at the command prompt:

chcp 65001

只要您已将窗口配置为使用包含字符的字体,这应该可以解决您的问题.

This should fix your issue, as long as you've configured the window to use a font that contains the character.

查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆