Python 将 json 转储到 yaml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15941996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dump json into yaml
提问by holys
I got a .jsonfile (named it meta.json) like this:
我有一个这样的.json文件(命名为meta.json):
{
"main": {
"title": "今日は雨が降って",
"description": "今日は雨が降って"
}
}
I would like to convert it to a .yamlfile (named it meta.yaml) like :
我想将它转换为一个.yaml文件(命名为meta.yaml),如:
title: "今日は雨が降って"
description: "今日は雨が降って"
What I have done was :
我所做的是:
import simplejson as json
import pyyaml
f = open('meta.json', 'r')
jsonData = json.load(f)
f.close()
ff = open('meta.yaml', 'w+')
yamlData = {'title':'', 'description':''}
yamlData['title'] = jsonData['main']['title']
yamlData['description'] = jsonData['main']['description']
yaml.dump(yamlData, ff)
# So you can see that what I need is the value of meta.json
But sadly, what I got is following:
但遗憾的是,我得到的是以下内容:
{description: "\u4ECA\u65E5\u306F\u96E8\u304C\u964D\u3063\u3066", title: "\u4ECA\u65E5\
\u306F\u96E8\u304C\u964D\u3063"}
Why?
为什么?
采纳答案by shoma
pyyaml.dump() has "allow_unicode" option, it's default is None, all non-ASCII characters in the output are escaped. If allow_unicode=True write raw unicode strings.
pyyaml.dump() 有“allow_unicode”选项,它的默认值为 None,输出中的所有非 ASCII 字符都被转义。如果 allow_unicode=True 写入原始 unicode 字符串。
yaml.dump(data, ff, allow_unicode=True)
bonus
奖金
json.dump(data, outfile, ensure_ascii=False)
回答by DhruvPathak
This is correct. The "\u...." strings are unicode representation of your Japanese? string. When you decode and use it with proper encoding, it should display fine wherever you use it. eg a webpage.
这是对的。"\u...." 字符串是你的日语的 unicode 表示?细绳。当您解码并以正确的编码使用它时,它应该在您使用它的任何地方都能正常显示。例如网页。
See the equality of data inspite of different representation as string :
尽管作为字符串的不同表示形式,请参阅数据的相等性:
>>> import json
>>> j = '{ "main": { "title": "今日は雨が降って", "description": "今日は雨が降って" }}'
>>> s = json.loads(j)
>>> t = json.dumps(s)
>>> j
'{ "main": { "title": "\xe4\xbb\x8a\xe6\x97\xa5\xe3\x81\xaf\xe9\x9b\xa8\xe3\x81\x8c\xe9\x99\x8d\xe3\x81\xa3\xe3\x81\xa6", "description": "\xe4\xbb\x8a\xe6\x97\xa5\xe3\x81\xaf\xe9\x9b\xa8\xe3\x81\x8c\xe9\x99\x8d\xe3\x81\xa3\xe3\x81\xa6" }}'
>>> t
'{"main": {"description": "\u4eca\u65e5\u306f\u96e8\u304c\u964d\u3063\u3066", "title": "\u4eca\u65e5\u306f\u96e8\u304c\u964d\u3063\u3066"}}'
>>> s == json.loads(t)
True
回答by root
In [1]: import json, yaml
In [2]: with open('test.json') as js:
...: data = json.load(js)[u'main']
...:
In [3]: with open('test.yaml', 'w') as yml:
...: yaml.dump(data, yml, allow_unicode=True)
...:
In [4]: ! cat test.yaml
{!!python/unicode 'description': 今日は雨が降って, !!python/unicode 'title': 今日は雨が降って}
In [5]: with open('test.yaml', 'w') as yml:
...: yaml.safe_dump(data, yml, allow_unicode=True)
...:
In [6]: ! cat test.yaml
{description: 今日は雨が降って, title: 今日は雨が降って}
回答by Saurabh Hirani
This works for me:
这对我有用:
#!/usr/bin/env python
import sys
import json
import yaml
print yaml.dump(yaml.load(json.dumps(json.loads(open(sys.argv[1]).read()))), default_flow_style=False)
So what we are doing is:
所以我们正在做的是:
- load json file through json.loads
- json loads in unicode format - convert that to string by json.dump
- load the yaml through yaml.load
- dump the same in a file through yaml.dump - default_flow_style - True displays data inline, False doesn't do inline - so you have dumpable data ready.
- 通过 json.loads 加载 json 文件
- json 以 unicode 格式加载 - 通过 json.dump 将其转换为字符串
- 通过 yaml.load 加载 yaml
- 通过 yaml.dump 将相同的内容转储到文件中 - default_flow_style - True 显示内联数据,False 不内联 - 所以你已经准备好可转储的数据。
Takes care of unicode as per How to get string objects instead of Unicode ones from JSON in Python?

