I got a .json
file (named it meta.json
) like this:
{
"main": {
"title": "今日は雨が降って",
"description": "今日は雨が降って"
}
}
I would like to convert it to a .yaml
file (named it meta.yaml
) like :
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?
pyyaml.dump()
has an allow_unicode
option that defaults to None
(all non-ASCII characters in the output are escaped). If allow_unicode=True
, then it writes raw Unicode strings.
yaml.dump(data, ff, allow_unicode=True)
You can dump JSON without encoding as follows:
json.dump(data, outfile, ensure_ascii=False)