Python YAML-JSON转换器;有缺陷吗?
我试图用Python实现一个简单的YAML到JSON转换器,但它似乎根本不正确。我是本地Perl/Ruby程序员,因此我有三个脚本和一个输入文件: testinput.yamlPython YAML-JSON转换器;有缺陷吗?,python,ruby,json,perl,yaml,Python,Ruby,Json,Perl,Yaml,我试图用Python实现一个简单的YAML到JSON转换器,但它似乎根本不正确。我是本地Perl/Ruby程序员,因此我有三个脚本和一个输入文件: testinput.yaml --- default: default: line_one: '[I]<description>[/I]' line_three: '<creator>' line_two: '<title> [<type>]' link_to: '&
---
default:
default:
line_one: '[I]<description>[/I]'
line_three: '<creator>'
line_two: '<title> [<type>]'
link_to: '<citation>'
use JSON;
use YAML;
my $filename = "testinput.yaml";
my $yaml = YAML::LoadFile($filename);
print encode_json($yaml);
{"default":{"default":{"line_three":"<creator>","line_two":"<title> [<type>]","link_to":"<citation>","line_one":"[I]<description>[/I]"}}}
y2j.pl
---
default:
default:
line_one: '[I]<description>[/I]'
line_three: '<creator>'
line_two: '<title> [<type>]'
link_to: '<citation>'
use JSON;
use YAML;
my $filename = "testinput.yaml";
my $yaml = YAML::LoadFile($filename);
print encode_json($yaml);
{"default":{"default":{"line_three":"<creator>","line_two":"<title> [<type>]","link_to":"<citation>","line_one":"[I]<description>[/I]"}}}
y2j.py
import yaml
import json
stream = open("testinput.yaml", 'r')
data = yaml.load_all(stream)
json = json.dumps(data)
print(json)
enter code here
Traceback (most recent call last):
File "y2j.py", line 7, in <module>
json = json.dumps(data)
File "/usr/lib64/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib64/python2.6/json/encoder.py", line 323, in _ iterencode_default
newobj = self.default(o)
File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <generator object load_all at 0x15a81e0> is not JSON serializable
然后输出:
ruby y2j.rb
{"default":{"default":{"link_to":"<citation>","line_two":"<title> [<type>]","line_three":"<creator>","line_one":"[I]<description>[/I]"}}}
{“default”:{“default”:{“link_to”:“,”line_two”:“[]”“,”line_two”:“,”line_two”:“,”line_one”:“[I][/I]”}}
perly2j.pl
---
default:
default:
line_one: '[I]<description>[/I]'
line_three: '<creator>'
line_two: '<title> [<type>]'
link_to: '<citation>'
use JSON;
use YAML;
my $filename = "testinput.yaml";
my $yaml = YAML::LoadFile($filename);
print encode_json($yaml);
{"default":{"default":{"line_three":"<creator>","line_two":"<title> [<type>]","link_to":"<citation>","line_one":"[I]<description>[/I]"}}}
{“default”:{“default”:{“第三行”:“,“第二行”:“[]”,“链接到”:“,“第一行”:“[I][/I]”}}
(到目前为止,一切顺利)
pythony2j.py
import yaml
import json
stream = open("testinput.yaml", 'r')
data = yaml.load_all(stream)
json = json.dumps(data)
print(json)
enter code here
Traceback (most recent call last):
File "y2j.py", line 7, in <module>
json = json.dumps(data)
File "/usr/lib64/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib64/python2.6/json/encoder.py", line 323, in _ iterencode_default
newobj = self.default(o)
File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <generator object load_all at 0x15a81e0> is not JSON serializable
回溯(最近一次呼叫最后一次):
文件“y2j.py”,第7行,在
json=json.dumps(数据)
文件“/usr/lib64/python2.6/json/_init__.py”,第230行,转储
返回默认编码器编码(obj)
文件“/usr/lib64/python2.6/json/encoder.py”,第367行,在encode中
chunks=list(self.iterencode(o))
文件“/usr/lib64/python2.6/json/encoder.py”,第317行,在iterencode中
对于self.\u iterencode\u默认值(o,标记):
文件“/usr/lib64/python2.6/json/encoder.py”,第323行,默认为iterencode
newobj=self.default(o)
默认情况下,文件“/usr/lib64/python2.6/json/encoder.py”第344行
raise TypeError(repr(o)+“不可JSON序列化”)
TypeError:不可序列化JSON
这里是否有我完全没有的东西?
yaml。根据错误消息,load\u all
是一个错误。请参见以下示例:
请注意,此代码在生成器上迭代以访问其内容
相反,您应该加载
数据(或者,为了降低安全风险,安全加载
)。因此,我认为您的Python版本应该如下所示:
import json
import yaml
with open("testinput.yaml") as stream:
yaml_data = yaml.safe_load(stream)
json_data = json.dumps(yaml_data)
print(json_data)
注意以下几点:
按照布局李>导入
- 使用
上下文管理器来处理文件;及和
- 使用名称
避免隐藏json\u data
库json
如果文件中有多个文档,可以尝试使用例如
yaml_data=list(yaml.load_all(stream))
UGH,谢谢,已更改,按预期工作。不要对变量名使用json
,它会隐藏(覆盖)json
模块。