我知道在SO上有一些关于这个的问题,但我找不到我想要的东西.
我正在使用pyyaml来读取(.load()
).yml
文件,修改或添加一个键,然后.dump()
再次写入它.问题是我想在转储后保留文件格式,但它会改变.
例如,我编辑键en.test.index.few
说"Bye"
,而不是"Hello"
蟒蛇:
with open(path, 'r', encoding = "utf-8") as yaml_file:
self.dict = pyyaml.load(yaml_file)
Run Code Online (Sandbox Code Playgroud)
然后,更改密钥:
with open(path, 'w', encoding = "utf-8") as yaml_file:
dump = pyyaml.dump(self.dict, default_flow_style = False, allow_unicode = True, encoding = None)
yaml_file.write( dump )
Run Code Online (Sandbox Code Playgroud)
YAML:
之前:
en:
test:
new: "Bye"
index:
few: "Hello"
anothertest: "Something"
Run Code Online (Sandbox Code Playgroud)
后:
en:
anothertest: Something
test:
index:
few: Hello
new: Bye
Run Code Online (Sandbox Code Playgroud)
有没有办法保持相同的格式?,例如qoutes和order.我使用了错误的工具吗?
我知道也许原始文件不完全正确,但我无法控制它(这是一个Ruby on Rails i18n文件).
非常感谢你.
我正在使用yaml.dump
输出字典.它根据键按字母顺序打印出每个项目.
>>> d = {"z":0,"y":0,"x":0}
>>> yaml.dump( d, default_flow_style=False )
'x: 0\ny: 0\nz: 0\n'
Run Code Online (Sandbox Code Playgroud)
有没有办法控制键/值对的顺序?
在我的特定用例中,反向打印(巧合)就足够了.但是为了完整性,我正在寻找一个答案,展示如何更精确地控制订单.
我看过使用collections.OrderedDict
但是PyYAML没有(似乎)支持它.我也看过子类化yaml.Dumper
,但我无法弄清楚它是否有能力改变项目顺序.
我在某种程度上得到它,但我还没有看到一个没有提出更多问题而不是答案的例子.
http://rhnh.net/2011/01/31/yaml-tutorial
# Set.new([1,2]).to_yaml
--- !ruby/object:Set
hash:
1: true
2: true
Run Code Online (Sandbox Code Playgroud)
我知道我们正在声明一个Set标签.我不知道后续哈希映射与它有什么关系.我们是在宣布架构吗?有人能给我看一个带有多个标签声明的例子吗?
我已经阅读了规范:http://yaml.org/spec/1.2/spec.html#id2761292
%TAG ! tag:clarkevans.com,2002:
Run Code Online (Sandbox Code Playgroud)
这是宣告架构吗?为了成功解析文件,解析器还需要做些什么吗?某种类型的模式文件?
http://www.yaml.org/refcard.html
Tag property: # Usually unspecified.
none : Unspecified tag (automatically resolved by application).
'!' : Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
'!foo' : Primary (by convention, means a local "!foo" tag).
'!!foo' : Secondary (by convention, means "tag:yaml.org,2002:foo").
'!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
'!<foo>': Verbatim tag (always means "foo").
Run Code Online (Sandbox Code Playgroud)
为什么与主标记和辅助标记相关,为什么辅助标记引用URI?有这些问题正在解决什么问题?
我似乎看到很多"他们是什么",没有"为什么他们在那里",或"他们用于什么".
PyYAML包将未标记的字符串作为unicode或str对象加载,具体取决于它们的内容.
我想在整个程序中使用unicode对象(不幸的是,还不能切换到Python 3).
是否有一种简单的方法可以强制PyYAML始终对字符串加载unicode对象?我不想用!!python/unicode
标签弄乱我的YAML .
# Encoding: UTF-8
import yaml
menu= u"""---
- spam
- eggs
- bacon
- crème brûlée
- spam
"""
print yaml.load(menu)
Run Code Online (Sandbox Code Playgroud)
输出: ['spam', 'eggs', 'bacon', u'cr\xe8me br\xfbl\xe9e', 'spam']
我想要: [u'spam', u'eggs', u'bacon', u'cr\xe8me br\xfbl\xe9e', u'spam']
我正在使用pyyaml将对象转储到文件中.对象中有几个unicode字符串.我以前做过这个,但现在它正在生成这样的输出项:
'item': !!python/unicode "some string"
Run Code Online (Sandbox Code Playgroud)
而不是期望的:
'item': 'some string'
Run Code Online (Sandbox Code Playgroud)
我打算输出为utf-8.我使用的当前命令是:
yaml.dump(data,file(suite_out,'w'),encoding='utf-8',indent=4,allow_unicode=True)
Run Code Online (Sandbox Code Playgroud)
在其他地方,我做以下工作:
codecs.open(suite_out,"w","utf-8").write(
yaml.dump(suite,indent=4,width=10000)
)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Python 2.7.3
我有一个带有短字符串属性的对象,以及一个长多行字符串属性.我想将短字符串写为YAML引用的标量,将多行字符串写为文字标量:
my_obj.short = "Hello"
my_obj.long = "Line1\nLine2\nLine3"
Run Code Online (Sandbox Code Playgroud)
我希望YAML看起来像这样:
short: "Hello"
long: |
Line1
Line2
Line3
Run Code Online (Sandbox Code Playgroud)
我怎样才能指示PyYAML这样做?如果我调用yaml.dump(my_obj)
它,它会产生一个类似dict的输出:
{long: 'line1
line2
line3
', short: Hello}
Run Code Online (Sandbox Code Playgroud)
(不确定为什么长的是这样的双倍间距...)
我可以指示PyYAML如何对待我的属性吗?我想影响顺序和风格.
如何格式化这样的YAML文档,以便PyYAML可以正确解析它?
Data: Some data, here and a special character like ':'
Another line of data on a separate line
Run Code Online (Sandbox Code Playgroud)
我知道':'字符是特殊的所以我必须用引号括起整个东西:
Data: "Some data, here and a special character like ':'
Another line of data on a separate line"
Run Code Online (Sandbox Code Playgroud)
为了添加新行,我必须添加'\n':
Data: "Some data, here and a special character like ':'\n
Another line of data on a separate line"
Run Code Online (Sandbox Code Playgroud)
有没有格式化YAML文档,所以我不必添加\n
's以便有一个新行?
我希望能够转储一个字典,其中包含我希望在块样式中具有的长字符串以便于阅读.例如:
foo: |
this is a
block literal
bar: >
this is a
folded block
Run Code Online (Sandbox Code Playgroud)
PyYAML支持使用这种样式加载文档,但我似乎找不到以这种方式转储文档的方法.我错过了什么吗?
我有一个字典列表,我想序列化:
list_of_dicts = [ { 'key_1': 'value_a', 'key_2': 'value_b'},
{ 'key_1': 'value_c', 'key_2': 'value_d'},
...
{ 'key_1': 'value_x', 'key_2': 'value_y'} ]
yaml.dump(list_of_dicts, file, default_flow_style = False)
Run Code Online (Sandbox Code Playgroud)
产生以下内容:
- key_1: value_a
key_2: value_b
- key_1: value_c
key_2: value_d
(...)
- key_1: value_x
key_2: value_y
Run Code Online (Sandbox Code Playgroud)
但我想得到这个:
- key_1: value_a
key_2: value_b
<-|
- key_1: value_c |
key_2: value_d | empty lines between blocks
(...) |
<-|
- key_1: value_x
key_2: value_y
Run Code Online (Sandbox Code Playgroud)
PyYAML 文档dump()
非常简短地讨论了参数,似乎没有关于这个特定主题的任何内容.
手动编辑文件以添加换行符可以提高可读性,之后结构仍然很好,但我不知道如何使dump方法生成它.
一般来说,除了简单的缩进之外,还有一种方法可以更好地控制输出格式吗?
当我使用YAML加载带有e形式的JSON转储的数字时,该数字将作为字符串而不是浮点数加载.
我想这个简单的例子可以解释我的问题.
import json
import yaml
In [1]: import json
In [2]: import yaml
In [3]: All = {'one':1,'low':0.000001}
In [4]: jAll = json.dumps(All)
In [5]: yAll = yaml.safe_load(jAll)
In [6]: yAll
Out[6]: {'low': '1e-06', 'one': 1}
Run Code Online (Sandbox Code Playgroud)
YAML将1e-06加载为字符串而不是数字?我该如何解决?
pyyaml ×10
python ×8
yaml ×7
dictionary ×2
json ×1
python-2.x ×1
python-3.x ×1
quotes ×1
tags ×1