yaml简介以及在python上的应用

YAML是一种直观的能够被电脑识别的的数据序列化格式,容易被人类阅读,并且容易和脚本语言交互。YAML类似于XML,但是语法比XML简单得多,对于转化成数组或可以hash的数据时是很简单有效的。

YAML语法规则:

  http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/

  http://www.yaml.org/

YAML被很多人认为是可以超越xml和json的文件格式。对比xml,除了拥有xml的众多优点外,它足够简单,易于使用。而对于json,YAML可以写成规范化的配置文件(这我认为是高于json很多的优点,用json写配置文件会让人发疯)。

YAML使用寄主语言的数据类型,这在多种语言中流传的时候可能会引起兼容性的问题。

如何写yaml?(抄的)

Yaml代码   收藏代码
  1. name: Tom Smith  
  2. age: 37  
  3. spouse:  
  4.     name: Jane Smith  
  5.     age: 25  
  6. children:  
  7.  - name: Jimmy Smith  
  8.    age: 15  
  9.  - name1: Jenny Smith  
  10.    age1: 12  

具体语法请参照yaml语法规则。

--------------------------------------------------------------------------------------------

yaml在python上的具体实现:PyYaml

将yaml写成配置脚本test.yaml ,以下介绍如何读写yaml配置。

使用python的yaml库PyYAML。http://pyyaml.org/

安装到python lib下后就可以正常使用了。

Python代码   收藏代码
  1. #加载yaml  
  2. import yaml  
  3.   
  4. #读取文件  
  5. f = open('test.yaml')  
  6.   
  7. #导入  
  8. x = yaml.load(f)  
  9.   
  10. print x  

也许你会得到以下类似的strings:

Python代码   收藏代码
  1. {'age'37'spouse': {'age'25'name''Jane Smith'}, 'name''Tom Smith''children': [{'age'15'name''Jimmy Smith'}, {'age1'12'name1''Jenny Smith'}]}  

python上使用yaml库很简单,基本就使用两个函数:

yaml.load

yaml.dump

对于使用过pickle的各位童鞋来说,这意味着什么不用详说了吧?

Warning: It is not safe to call yaml.load with any data received from an untrusted source!yaml.load is as powerful as pickle.load and so may call any Python function.

对于yaml的读取来讲,最难的在于写出正确的yaml数据格式。如果一不小心出错,将会导致load异常,但有时没有异常报,而是会读不出任何数据。

pyYaml是完全的python实现,号称比pickle更nb。(这谁知道呢?)

yaml.load accepts a byte string, a Unicode string, an open binary file object, or an open text file object. A byte string or a file must be encoded withutf-8, utf-16-be or utf-16-le encoding.yaml.load detects the encoding by checking the BOM (byte order mark) sequence at the beginning of the string/file. If noBOM is present, the utf-8 encoding is assumed.

yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象。字节字符串和文件必须是utf-8,utf-16-be或utf-16-le编码的.yaml.load通过检查字符串/文件开始的BOM(字节序标记)来确认编码。如果没有BOM,就默认为utf-8。

 

百度上的关于BOM
    在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符"ZERO WIDTH NO-BREAK SPACE"。这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little- Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
    UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。Windows就是使用BOM来标记文本文件的编码方式的。

yaml.load 会返回一个python对象。关于会是什么……看你数据是什么了……

If a string or a file contains several documents, you may load them all with theyaml.load_all function.

如果string或文件包含几块yaml文档,你可以使用yaml.load_all来解析全部的文档。

Python代码   收藏代码
  1. yaml.load(stream, Loader=<class 'yaml.loader.Loader'>)  
  2.     Parse the first YAML document in a stream #只解析第一个  
  3.     and produce the corresponding Python object.  
  4.   
  5. yaml.load_all(stream, Loader=<class 'yaml.loader.Loader'>)  
  6.     Parse all YAML documents in a stream  
  7.     and produce corresponding Python objects.  

yaml.load_all 会生成一个迭代器,你要做的就是for 读出来

 

Python代码   收藏代码
  1. documents = """ 
  2. name: The Set of Gauntlets 'Pauraegen' 
  3. description: > 
  4.   A set of handgear with sparks that crackle 
  5.   across its knuckleguards. 
  6.  --- 
  7. name: The Set of Gauntlets 'Paurnen' 
  8. description: > 
  9.    A set of gauntlets that gives off a foul, 
  10.    acrid odour yet remains untarnished. 
  11.  --- 
  12. name: The Set of Gauntlets 'Paurnimmen' 
  13. description: > 
  14.    A set of handgear, freezing with unnatural cold. 
  15. """  
  16.   
  17.   
  18. for data in yaml.load_all(documents):  
  19. print data  
  20.   
  21. #{'description': 'A set of handgear with sparks that crackle across its #knuckleguards.\n',  
  22. #'name': "The Set of Gauntlets 'Pauraegen'"}  
  23. #{'description': 'A set of gauntlets that gives off a foul, acrid odour #yet remains untarnished.\n',  
  24. #'name': "The Set of Gauntlets 'Paurnen'"}  
  25. #{'description': 'A set of handgear, freezing with unnatural cold.\n',  
  26. #'name': "The Set of Gauntlets 'Paurnimmen'"}  

PyYAML allows you to construct a Python object of any type.

Even instances of Python classes can be constructed using the !!python/object tag.

PyYaml允许你构建任何类型的python对象,甚至是python类实例,只需要借助一下yaml标签!!python/object。

这个以后再说,非常有用的东西。

Note that the ability to construct an arbitrary Python object may be dangerous if you receive a YAML document from an untrusted source such as Internet. The functionyaml.safe_load limits this ability to simple Python objects like integers or lists.

需要注意的是随意在yaml里构建python对象是有一定危险的,尤其是接收到一个未知的yaml文档。yaml.safe_load可以限制这个能力,就使用些简单的对象吧。

 ---------------------------------------

Dumping YAML

The yaml.dump function accepts a Python object and produces a YAML document.

yaml.dump 将一个python对象生成为yaml文档,与yaml.load搭配使用。

Python代码   收藏代码
  1. dump(data, stream=None, Dumper=<class 'yaml.dumper.Dumper'>, **kwds)  
  2.   
  3.     Serialize a Python object into a YAML stream.  
  4.     If stream is Nonereturn the produced string instead.  
  5.     #很好,如果缺省数据流为空的话,就会给你返回个字符串作为yaml文档  
Python代码   收藏代码
  1. aproject = {'name''Silenthand Olleander',   
  2.                    'race''Human',  
  3.                     'traits': ['ONE_HAND''ONE_EYE']  
  4.                    }  
  5.   
  6.   
  7. print yaml.dump(aproject)  
  8.   
  9. #返回  
  10. #name: Silenthand Olleander  
  11. #race: Human  
  12. #traits: [ONE_HAND, ONE_EYE]  

yaml.dump accepts the second optional argument, which must be an open text or binary file. In this case,yaml.dump will write the produced YAML document into the file. Otherwise,yaml.dump returns the produced document. 

解释上面那句话的:yaml.dump接收的第二个参数一定要是一个打开的文本文件或二进制文件,yaml.dump会把生成的yaml文档写到文件里。否则,yaml.dump会返回生成的文档。

If you need to dump several YAML documents to a single stream, use the functionyaml.dump_all. yaml.dump_all accepts a list or a generator producing

Python objects to be serialized into a YAML document. The second optional argument is an open file.

如果你需要把几段yaml文档同时写进一个数据流中,请使用yaml.dump_all函数。yaml.dump_all可以接收一个列表或者生成python对象的可序列化生成器(好别扭啊),第二个参数是打开的文件。这完全是对应yaml.load_all的。

You may even dump instances of Python classes.

你甚至可以直接把python类的实例(对象)dump进去。

yaml.dump supports a number of keyword arguments that specify formatting details for the emitter. For instance, you may set the preferred intendation and width, use the canonical YAML format or force preferred style for scalars and collections.

yaml.dump支持很多种确定格式化发射器的关键字参数(请先无视这句- -#)。比如你可以设置缩进和宽度(指的yaml文档),使用标准yaml格式或者强制优先样式对于标量和收集(请继续无视- -#)。

瞧这翻译的。

Python代码   收藏代码
  1. dump_all(documents, stream=None, Dumper=<class 'yaml.dumper.Dumper'>, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding='utf-8', explicit_start=None, explicit_end=None, version=None, tags=None)  
  2.   
  3.   
  4. #不过对应具体的函数参数可以看出所叙述的几个参数  
  5. #cannonical  
  6. #indent  
  7. #width  
  8. #等等  

举例

Python代码   收藏代码
  1. >>> print yaml.dump(range(50))  
  2. [012345678910111213141516171819202122,  
  3.   2324252627282930313233343536373839404142,  
  4.   43444546474849]  
  5.   
  6. >>> print yaml.dump(range(50), width=50, indent=4)  
  7. [0123456789101112131415,  
  8.     161718192021222324252627,  
  9.     282930313233343536373839,  
  10.     40414243444546474849]  
  11.   
  12. >>> print yaml.dump(range(5), canonical=True)  
  13. ---  
  14. !!seq [  
  15.   !!int "0",  
  16.   !!int "1",  
  17.   !!int "2",  
  18.   !!int "3",  
  19.   !!int "4",  
  20. ]  
  21.   
  22. >>> print yaml.dump(range(5), default_flow_style=False)  
  23. 0  
  24. 1  
  25. 2  
  26. 3  
  27. 4  
  28.   
  29. >>> print yaml.dump(range(5), default_flow_style=True, default_style='"')  
  30. [!!int "0", !!int "1", !!int "2", !!int "3", !!int "4"]  

这关键都在后面的参数呢。

------------------------------------------------------

Constructors, representers, resolvers

构造器,描绘器(?),解析器

You may define your own application-specific tags. The easiest way to do it is to define a subclass ofyaml.YAMLObject

你可以自定义一个程序专属标签(tag),定义一个yaml.YAMLObject的子类的最简单方法可以这么干:

Python代码   收藏代码
  1. class Monster(yaml.YAMLObject):  
  2.     yaml_tag = u'!Monster'  
  3.     def __init__(self, name, hp, ac, attacks):  
  4.         self.name = name  
  5.         self.hp = hp  
  6.         self.ac = ac  
  7.         self.attacks = attacks  
  8.     def __repr__(self):  
  9.         return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % (  
  10.             self.__class__.__name__, self.name, self.hp, self.ac,self.attacks)  

The above definition is enough to automatically load and dump Monster objects:

上面这个定义的Monster类已经足够用来load和dump了:

Python代码   收藏代码
  1. >>> yaml.load(""" 
  2. ... --- !Monster 
  3. ... name: Cave spider 
  4. ... hp: [2,6]    # 2d6 
  5. ... ac: 16 
  6. ... attacks: [BITE, HURT] 
  7. ... """)  
  8.   
  9. Monster(name='Cave spider', hp=[26], ac=16, attacks=['BITE''HURT'])  
  10.   
  11. >>> print yaml.dump(Monster(  
  12. ...     name='Cave lizard', hp=[3,6], ac=16, attacks=['BITE','HURT']))  
  13.   
  14. !Monster  
  15. ac: 16  
  16. attacks: [BITE, HURT]  
  17. hp: [36]  
  18. name: Cave lizard  

yaml.YAMLObject uses metaclass magic to register a constructor, which transforms a YAML node to a class instance, and a representer, which serializes a class instance to a YAML node.

yaml.YAMLObject 使用魔法元类注册一个把yaml编码转成类实例的构造器,还有一个把类实例序列化成yaml编码的描述器。

If you don't want to use metaclasses, you may register your constructors and representers using the functionsyaml.add_constructor and yaml.add_representer. For instance, you may want to add a constructor and a representer for the followingDice class:

如果不想使用元类,也可以使用函数yaml.add_constructor和yaml.add_representer来注册构造器和描述器。例如,你可以把一个构造器和描述器加到下面这个Dice类里:

Python代码   收藏代码
  1. >>> class Dice(tuple):  
  2. ...     def __new__(cls, a, b):  
  3. ...         return tuple.__new__(cls, [a, b])  
  4. ...     def __repr__(self):  
  5. ...         return "Dice(%s,%s)" % self  
  6.   
  7. >>> print Dice(3,6)  
  8. Dice(3,6)  

The default representation for Dice objects is not nice:

这个Dice对象默认的yaml描述可不怎么好看:

Python代码   收藏代码
  1. >>> print yaml.dump(Dice(3,6))  
  2.   
  3. !!python/object/new:__main__.Dice  
  4. - !!python/tuple [36]  

Suppose you want a Dice object to represented as AdB in YAML:

好,现在假设你想把Dice对象描述成在yaml里为"AdB"的形式(A,B为变量)。

First we define a representer that convert a dice object to scalar node with the tag!dice and register it.

首先我们定义一个可以把Dice对象转换成带有'!dice'标签节点的描述器,然后注册。

Python代码   收藏代码
  1. >>> def dice_representer(dumper, data):  
  2. ...     return dumper.represent_scalar(u'!dice', u'%sd%s' % data)  
  3.   
  4. >>> yaml.add_representer(Dice, dice_representer)  

Now you may dump an instance of the Dice object:

现在你就可以dump一个Dice实例了:

Python代码   收藏代码
  1. >>> print yaml.dump({'gold': Dice(10,6)})  
  2. {gold: !dice '10d6'}  

Let us add the code to construct a Dice object:

让我们把节点加到Dice对象的构造器中。

Python代码   收藏代码
  1. >>> def dice_constructor(loader, node):  
  2. ...     value = loader.construct_scalar(node)  
  3. ...     a, b = map(int, value.split('d'))  
  4. ...     return Dice(a, b)  
  5.   
  6. >>> yaml.add_constructor(u'!dice', dice_constructor)  

Then you may load a Dice object as well:

然后就可以使用了

Python代码   收藏代码
  1. >>> print yaml.load(""" 
  2. ... initial hit points: !dice 8d4 
  3. ... """)  
  4.   
  5. {'initial hit points': Dice(8,4)}  

从这里可以看出了,constructor和representer是相对的,一个为load,一个为dump。


#######官方文档####

http://pyyaml.org/wiki/PyYAMLDocumentation


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 PyYAML 库将 YAML 格式的字符串转换成 Python 字典。以下是一个示例代码: ```python import yaml yaml_str = """ name: John age: 30 address: street: 123 Main St city: Anytown state: CA """ dict_obj = yaml.safe_load(yaml_str) print(dict_obj) ``` 输出: ``` {'name': 'John', 'age': 30, 'address': {'street': '123 Main St', 'city': 'Anytown', 'state': 'CA'}} ``` 这里使用了 `yaml.safe_load()` 方法来安全地加载 YAML 字符串并将其转换成 Python 字典格式。注意,PyYAML 库需要安装才能使用。 ### 回答2: Python中可以使用PyYAML模块来实现将YAML格式数据转换为字典。 首先,需要安装PyYAML模块。可以使用pip install pyyaml命令来安装,或者在Python的环境中找到合适的方式安装。 接下来,导入PyYAML模块,使用yaml.load()函数来将YAML数据转换为字典。示例如下: ```python import yaml yaml_data = """ name: John age: 25 country: USA """ data_dict = yaml.load(yaml_data) print(data_dict) ``` 以上代码中,首先定义了一个YAML格式的数据字符串。然后使用yaml.load()函数将其转换为字典,并将结果赋值给data_dict变量。最后打印data_dict的结果。 运行以上代码,会输出以下结果: ```python {'name': 'John', 'age': 25, 'country': 'USA'} ``` 可以看到,YAML格式的数据已成功转换为字典。 在实际应用中,可以根据具体需求对YAML数据进行处理。例如,可以通过字典的key来访问和修改对应的值,或者根据具体场景进行进一步的处理。 ### 回答3: Python中使用PyYAML库可以将YAML数据转换为字典。 首先,需要安装PyYAML库。可以使用以下命令来安装: ``` pip install pyyaml ``` 接下来,通过以下代码示例来演示如何将YAML数据转换为字典: ```python import yaml # 定义一个YAML字符串 yaml_str = ''' name: John Smith age: 30 address: street: 123 Street city: Cityville state: State ''' # 使用load()函数将YAML转换为字典 data_dict = yaml.load(yaml_str, Loader=yaml.FullLoader) # 打印转换后的字典 print(data_dict) ``` 以上代码将输出如下字典数据: ``` {'name': 'John Smith', 'age': 30, 'address': {'street': '123 Street', 'city': 'Cityville', 'state': 'State'}} ``` 此时,YAML数据已经成功转换为字典。 需要注意的是,在PyYAML库的load()函数中,我们使用了`Loader=yaml.FullLoader`参数来指定加载器,以确保加载器为安全模式。这样可以防止潜在的代码注入问题。 通过上述步骤,我们可以将YAML数据转换为Python中的字典类型,方便后续的操作和数据处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值