一些可读内容,但无法 JSON 转储到文件
Some readble content, but impossible to JSON dump to file
这个text file(只有30个字节,内容是'(Ne pas r\xe9pondre a ce message)'
)可以打开插入一个dict
成功:
import json
d = {}
with open('temp.txt', 'r') as f:
d['blah'] = f.read()
with open('test.txt', 'w') as f:
data = json.dumps(d)
f.write(data)
但是不可能将 dict
转储到 JSON 文件中(请参阅下面的回溯)。为什么?
我尝试了很多由各种 SO 问题提供的解决方案。我能得到的最接近的解决方案是 this answer。使用它时,我可以转储到文件,但是 JSON 文件看起来像这样:
# test.txt
{"blah": "(Ne pas r\u00e9pondre a ce message)"}
而不是
# test.txt
{"blah": "(Ne pas répondre a ce message)"}
回溯:
File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 9: invalid continuation byte
[Finished in 0.1s with exit code 1]
您的文件不是 UTF-8 编码的。它使用 Latin 编解码器,如 ISO-8859-1 或 Windows Codepage 1252。读取该文件可为您提供 encoded 文本。
JSON 但是需要 Unicode 文本。 Python2有单独的Unicode类型,字节串(类型str
)需要使用合适的编解码器进行解码。 json.dumps()
函数默认使用UTF-8; UTF-8 是一种广泛使用的编码 Unicode 文本数据的编解码器,可以处理标准中的所有代码点,也是 JSON 字符串使用的默认编解码器(JSON 需要 文档以 3 种 UTF 编解码器之一进行编码)。
您需要手动解码字符串或告诉 json.dumps()
字节字符串使用什么编解码器:
data = json.dumps(d, encoding='latin1') # applies to all bytestrings in d
或
d['blah'] = d['blah'].decode('latin1')
data = json.dumps(d)
或在阅读时使用io.open()
解码:
import io
with io.open('test.txt', 'w', encoding='latin1') as f:
d['blah'] = f.read()
默认情况下,json
库使用 JSON 标准允许的 \uhhhh
转义语法生成 ASCII 安全 JSON 输出。 这是完全正常的,输出是有效的JSON并且可以被任何兼容的JSON解码器读取。
如果您必须 生成没有 \uhhhh
转义序列的 UTF-8 编码输出,请参阅 Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence
这个text file(只有30个字节,内容是'(Ne pas r\xe9pondre a ce message)'
)可以打开插入一个dict
成功:
import json
d = {}
with open('temp.txt', 'r') as f:
d['blah'] = f.read()
with open('test.txt', 'w') as f:
data = json.dumps(d)
f.write(data)
但是不可能将 dict
转储到 JSON 文件中(请参阅下面的回溯)。为什么?
我尝试了很多由各种 SO 问题提供的解决方案。我能得到的最接近的解决方案是 this answer。使用它时,我可以转储到文件,但是 JSON 文件看起来像这样:
# test.txt
{"blah": "(Ne pas r\u00e9pondre a ce message)"}
而不是
# test.txt
{"blah": "(Ne pas répondre a ce message)"}
回溯:
File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 9: invalid continuation byte
[Finished in 0.1s with exit code 1]
您的文件不是 UTF-8 编码的。它使用 Latin 编解码器,如 ISO-8859-1 或 Windows Codepage 1252。读取该文件可为您提供 encoded 文本。
JSON 但是需要 Unicode 文本。 Python2有单独的Unicode类型,字节串(类型str
)需要使用合适的编解码器进行解码。 json.dumps()
函数默认使用UTF-8; UTF-8 是一种广泛使用的编码 Unicode 文本数据的编解码器,可以处理标准中的所有代码点,也是 JSON 字符串使用的默认编解码器(JSON 需要 文档以 3 种 UTF 编解码器之一进行编码)。
您需要手动解码字符串或告诉 json.dumps()
字节字符串使用什么编解码器:
data = json.dumps(d, encoding='latin1') # applies to all bytestrings in d
或
d['blah'] = d['blah'].decode('latin1')
data = json.dumps(d)
或在阅读时使用io.open()
解码:
import io
with io.open('test.txt', 'w', encoding='latin1') as f:
d['blah'] = f.read()
默认情况下,json
库使用 JSON 标准允许的 \uhhhh
转义语法生成 ASCII 安全 JSON 输出。 这是完全正常的,输出是有效的JSON并且可以被任何兼容的JSON解码器读取。
如果您必须 生成没有 \uhhhh
转义序列的 UTF-8 编码输出,请参阅 Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence