Python 2.7 JSON转储UnicodeEncodeError

时间:2015-03-03 05:23:22

标签: python json python-2.7 dump

我有一个文件,其中每一行都是json对象,如下所示:

{"name": "John", ...}

{...}

我正在尝试使用相同的对象创建一个新文件,但是从所有这些文件中删除了某些属性。

当我这样做时,我得到一个UnicodeEncodeError。奇怪的是,如果我改为循环range(n)(对于某些数字n)并使用infile.next(),它就像我想要的那样工作。

为什么这样?如何通过迭代infile来实现这一点?我尝试使用dumps()代替dump(),但这只会在outfile中产生一堆空行。

with open(filename, 'r') as infile:
    with open('_{}'.format(filename), 'w') as outfile:
        for comment in infile:
            decodedComment = json.loads(comment)
            for prop in propsToRemove:
                # use pop to avoid exception handling
                decodedComment.pop(prop, None)
            json.dump(decodedComment, outfile, ensure_ascii = False)
            outfile.write('\n')

这是错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f47d' in position 1: ordinal not in range(128)

感谢您的帮助!

1 个答案:

答案 0 :(得分:13)

您遇到的问题是标准file.write()函数(由json.dump()函数调用)不支持unicode字符串。从错误消息中可以看出,您的字符串包含UTF字符\U0001f47d(结果是代码 EXTRATERRESTRIAL ALIEN ,谁知道?),以及可能还有其他UTF字符。要处理这些字符,您可以将它们编码为ASCII编码(它们将在输出文件中显示为\XXXXXX),或者您需要使用可以处理unicode的文件编写器。

要执行第一个选项,请使用以下行替换您的书写行:

json.dump(unicode(decodedComment), outfile, ensure_ascii = False)

第二个选项可能更符合您的要求,一个简单的选择是使用codecs模块。导入它,并将第二行更改为:

with codecs.open('_{}'.format(filename), 'w', encoding="utf-8") as outfile:

然后,您将能够以原始形式保存特殊字符。