YAML-JSON转换器;有什么缺陷吗?
YAML-JSON converter; something flawed?
我试图获得一个简单的 YAML 到 JSON 的转换器,但它似乎根本不正确。我是本地 Perl/Ruby 程序员,所以我有三个脚本和一个输入文件:
testinput.yaml
---
default:
default:
line_one: '[I]<description>[/I]'
line_three: '<creator>'
line_two: '<title> [<type>]'
link_to: '<citation>'
和一个 Python/Perl/Ruby 脚本,每个脚本都在我的脑海中做完全相同的事情:
y2j.rb
require 'rubygems'
require 'json'
require 'yaml'
yml = YAML.load_file('testinput.yaml')
json = JSON.dump(yml)
puts json
y2j.pl
use JSON;
use YAML;
my $filename = "testinput.yaml";
my $yaml = YAML::LoadFile($filename);
print encode_json($yaml);
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
然后输出:
ruby y2j.rb
{"default":{"default":{"link_to":"<citation>","line_two":"<title> [<type>]","line_three":"<creator>","line_one":"[I]<description>[/I]"}}}
perl y2j.pl
{"default":{"default":{"line_three":"<creator>","line_two":"<title> [<type>]","link_to":"<citation>","line_one":"[I]<description>[/I]"}}}
(到目前为止,还不错)
python y2j.py
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
我在这里遗漏了什么完全明显的东西吗?
yaml.load_all
,根据错误消息,是 generator. See the following example from the documentation:
>>> for data in yaml.load_all(documents):
... print data
{'description': 'A set of handgear with sparks that crackle across its knuckleguards.\n',
'name': "The Set of Gauntlets 'Pauraegen'"}
{'description': 'A set of gauntlets that gives off a foul, acrid odour yet remains untarnished.\n',
'name': "The Set of Gauntlets 'Paurnen'"}
{'description': 'A set of handgear, freezing with unnatural cold.\n',
'name': "The Set of Gauntlets 'Paurnimmen'"}
请注意,此代码遍历 生成器以访问其内容。
相反,您应该 load
数据(或者,为了降低安全风险,safe_load
)。因此,我认为您的 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)
注意以下几点:
import
按 the style guide; 布局
- 使用
with
上下文管理器来处理文件;和
- 使用名称
json_data
以避免隐藏 json
库。
如果您将在文件中有多个文档,您可以尝试例如yaml_data = list(yaml.load_all(stream))
.
我试图获得一个简单的 YAML 到 JSON 的转换器,但它似乎根本不正确。我是本地 Perl/Ruby 程序员,所以我有三个脚本和一个输入文件:
testinput.yaml
---
default:
default:
line_one: '[I]<description>[/I]'
line_three: '<creator>'
line_two: '<title> [<type>]'
link_to: '<citation>'
和一个 Python/Perl/Ruby 脚本,每个脚本都在我的脑海中做完全相同的事情:
y2j.rb
require 'rubygems'
require 'json'
require 'yaml'
yml = YAML.load_file('testinput.yaml')
json = JSON.dump(yml)
puts json
y2j.pl
use JSON;
use YAML;
my $filename = "testinput.yaml";
my $yaml = YAML::LoadFile($filename);
print encode_json($yaml);
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
然后输出:
ruby y2j.rb
{"default":{"default":{"link_to":"<citation>","line_two":"<title> [<type>]","line_three":"<creator>","line_one":"[I]<description>[/I]"}}}
perl y2j.pl
{"default":{"default":{"line_three":"<creator>","line_two":"<title> [<type>]","link_to":"<citation>","line_one":"[I]<description>[/I]"}}}
(到目前为止,还不错)
python y2j.py
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
我在这里遗漏了什么完全明显的东西吗?
yaml.load_all
,根据错误消息,是 generator. See the following example from the documentation:
>>> for data in yaml.load_all(documents):
... print data
{'description': 'A set of handgear with sparks that crackle across its knuckleguards.\n',
'name': "The Set of Gauntlets 'Pauraegen'"}
{'description': 'A set of gauntlets that gives off a foul, acrid odour yet remains untarnished.\n',
'name': "The Set of Gauntlets 'Paurnen'"}
{'description': 'A set of handgear, freezing with unnatural cold.\n',
'name': "The Set of Gauntlets 'Paurnimmen'"}
请注意,此代码遍历 生成器以访问其内容。
相反,您应该 load
数据(或者,为了降低安全风险,safe_load
)。因此,我认为您的 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)
注意以下几点:
import
按 the style guide; 布局
- 使用
with
上下文管理器来处理文件;和 - 使用名称
json_data
以避免隐藏json
库。
如果您将在文件中有多个文档,您可以尝试例如yaml_data = list(yaml.load_all(stream))
.