我有一个.json
文件(名为meta.json
),如下所示:
{
"main": {
"title": "今日は雨が降って",
"description": "今日は雨が降って"
}
}
我想将其转换为.yaml
文件(名为meta.yaml
),如:
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
但遗憾的是,我得到的是:
{description: "\u4ECA\u65E5\u306F\u96E8\u304C\u964D\u3063\u3066", title: "\u4ECA\u65E5\
\u306F\u96E8\u304C\u964D\u3063"}
为什么?
答案 0 :(得分:15)
pyyaml.dump()有“allow_unicode”选项,默认为None, 输出中的所有非ASCII字符都被转义。如果allow_unicode = True,则写入原始unicode字符串。
yaml.dump(data, ff, allow_unicode=True)
奖金
json.dump(data, outfile, ensure_ascii=False)
答案 1 :(得分:10)
这对我有用:
#!/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)
所以我们正在做的是:
根据How to get string objects instead of Unicode ones from JSON in Python?
来处理unicode答案 2 :(得分:2)
这是对的。 " \ u ...."字符串是日语的unicode表示?串。当您使用正确的编码进行解码和使用时,无论您在何处使用它,它都应该显示正常。例如一个网页。
尽管字符串的表示形式不同,但请注意数据的相等性:
>>> 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
答案 3 :(得分:1)
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: 今日は雨が降って}
答案 4 :(得分:1)
我很简单:
Undefined symbols for architecture arm64:
"firebase::FutureHandle::FutureHandle(firebase::FutureHandle const&)", referenced from:
firebase::remote_config::Fetch(unsigned long long) in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
____ZN8firebase13remote_config5FetchEy_block_invoke in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
___copy_helper_block_ea8_40c37_ZTSKN8firebase16SafeFutureHandleIvEE in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::FutureHandle::Detach()", referenced from:
firebase::remote_config::Fetch(unsigned long long) in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::FutureHandle::FutureHandle(unsigned long, firebase::detail::FutureApiInterface*)", referenced from:
_Firebase_RemoteConfig_Fetch__SWIG_1 in libFirebaseCppRemoteConfig.a(remote_config_08c4a1f5237ed5b16a396c81df6a9ee4.o)
firebase::remote_config::FetchLastResult() in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::FutureHandle::FutureHandle()", referenced from:
_Firebase_RemoteConfig_Fetch__SWIG_1 in libFirebaseCppRemoteConfig.a(remote_config_08c4a1f5237ed5b16a396c81df6a9ee4.o)
firebase::remote_config::FetchLastResult() in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::ReferenceCountedFutureImpl::CompleteHandle(firebase::FutureHandle const&)", referenced from:
void firebase::ReferenceCountedFutureImpl::CompleteInternal<void, void firebase::ReferenceCountedFutureImpl::CompleteInternal<void>(firebase::FutureHandle const&, int, char const*)::'lambda'(void*)>(firebase::FutureHandle const&, int, char const*, void firebase::ReferenceCountedFutureImpl::CompleteInternal<void>(firebase::FutureHandle const&, int, char const*)::'lambda'(void*) const&) in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::ReferenceCountedFutureImpl::ReleaseMutexAndRunCallbacks(firebase::FutureHandle const&)", referenced from:
void firebase::ReferenceCountedFutureImpl::CompleteInternal<void, void firebase::ReferenceCountedFutureImpl::CompleteInternal<void>(firebase::FutureHandle const&, int, char const*)::'lambda'(void*)>(firebase::FutureHandle const&, int, char const*, void firebase::ReferenceCountedFutureImpl::CompleteInternal<void>(firebase::FutureHandle const&, int, char const*)::'lambda'(void*) const&) in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::FutureHandle::operator=(firebase::FutureHandle const&)", referenced from:
_Firebase_RemoteConfig_Fetch__SWIG_1 in libFirebaseCppRemoteConfig.a(remote_config_08c4a1f5237ed5b16a396c81df6a9ee4.o)
firebase::remote_config::FetchLastResult() in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
"firebase::FutureHandle::~FutureHandle()", referenced from:
_Firebase_RemoteConfig_Fetch__SWIG_1 in libFirebaseCppRemoteConfig.a(remote_config_08c4a1f5237ed5b16a396c81df6a9ee4.o)
firebase::remote_config::Fetch(unsigned long long) in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
firebase::remote_config::FetchLastResult() in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
____ZN8firebase13remote_config5FetchEy_block_invoke in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
___destroy_helper_block_ea8_40c37_ZTSKN8firebase16SafeFutureHandleIvEE in libFirebaseCppRemoteConfig.a(remote_config_ios_e6d2ed559f32c182ac8412737f5fb36a.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)