将json转储到Yaml中 [英] dump json into yaml

查看:142
本文介绍了将json转储到Yaml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个 .json 文件(命名为 meta.json ),如下所示:

I got a .json file (named it meta.json) like this:

{
    "main": {
        "title": "今日は雨が降って",
        "description": "今日は雨が降って"
    }
}

我想将其转换为 .yaml 文件(命名为 meta.yaml ),例如:

I would like to convert it to a .yaml file (named it meta.yaml) like :

title: "今日は雨が降って"
description: "今日は雨が降って"

我所做的是:

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     


$ b $的值b

但可悲的是,我得到的只是欠:

But sadly, what I got is following:

{description: "\u4ECA\u65E5\u306F\u96E8\u304C\u964D\u3063\u3066", title: "\u4ECA\u65E5\
\u306F\u96E8\u304C\u964D\u3063"}

为什么?

推荐答案

pyyaml.dump()具有 allow_unicode选项,默认为None,
输出中的所有非ASCII字符均被转义。如果allow_unicode = True写入原始unicode字符串。

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.

yaml.dump(data, ff, allow_unicode=True)

奖金

json.dump(data, outfile, ensure_ascii=False)

这篇关于将json转储到Yaml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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