Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON确保编码_Python_Json_Python 2.7 - Fatal编程技术网

Python JSON确保编码

Python JSON确保编码,python,json,python-2.7,Python,Json,Python 2.7,我创建了一个JSON文件,其中包含特殊字符作为字符串 例如,我创建了一个带有 dict[u"Züge"] = ... json.dump(dict, file) 当我查看转储的文件时,它如下所示: { "Z\u00fcge": [ { 是否有方法将编码写入文件,以便任何文本编辑器自动显示正确的字符 然后我得到以下错误: UnicodeEncodeError:“ascii”编解码器无法在中编码字符u'\xfc' 位置2:序号不在范围内(128) 您需要在json.dump函数中使用

我创建了一个JSON文件,其中包含特殊字符作为字符串

例如,我创建了一个带有

dict[u"Züge"] = ...
json.dump(dict, file)
当我查看转储的文件时,它如下所示:

{
  "Z\u00fcge": [
    {
是否有方法将编码写入文件,以便任何文本编辑器自动显示正确的字符

然后我得到以下错误:

UnicodeEncodeError:“ascii”编解码器无法在中编码字符u'\xfc' 位置2:序号不在范围内(128)


您需要在
json.dump
函数中使用
确保_ascii=False

dict[u"Züge"] = ...
json.dump(dict, file,ensure_ascii=False)
:

在RFC允许的情况下(尽管不是必需的),此模块的序列化程序集在默认情况下确保_ascii=True,从而转义输出,以便生成的字符串仅包含ascii字符


调用时指定
确保\u ascii=False


是的,但是这里没有提到如何克服错误:(
json.dump(dict, file, ensure_ascii=False)