2015-12-15 61 views
0

使用PyYAML將包含值YES,NO的字典轉儲到YAML時,YES/NO引用爲'YES'或'NO'。如何通過外引號獲得YES或NO?是或否在PyYAML中傾銷爲'是'或'否'

+0

你有沒有試過不使用字符串? –

+0

例如,import yaml d = {'sample':'yes'} print yaml.dump(d) 結果是{sample:'yes'}。轉儲時,yes/no true/false值在引用之上。但我需要結果作爲{samle:是} –

+0

如果你需要'samle' /'sample'的值作爲布爾值加載(這是你加載'{sample:yes}時得到的結果',爲什麼不用你是否首先寫出一個布爾值,或者你需要寫出符合YAML 1.2的輸出(而不是YAML 1.1,這是PyYAML給你的),如果你解釋爲什麼**你會有幫助需要寫出字符串「Yes」作爲布爾值 – Anthon

回答

0

如果你想甩YES/yes/YesNO/no/No非引用標量,你必須做出一個解析器(代碼使用由PyYAML自卸車和PyYAML裝載機),以類似方式爲my answer here可防止從YAML將這些值作爲布爾值加載。

import re 
import yaml as yaml 
from yaml.resolver import BaseResolver, Resolver 
from yaml.emitter import Emitter 
from yaml.serializer import Serializer 
from yaml.representer import Representer as Repr 


class MyResolver(BaseResolver): 
    pass 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:bool', 
    re.compile(u'''^(?:true|True|TRUE|false|False|FALSE)$''', re.X), 
    list(u'tTfF')) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:float', 
    re.compile(u'''^(?: 
    [-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)? 
    |[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+) 
    |\\.[0-9_]+(?:[eE][-+][0-9]+)? 
    |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]* 
    |[-+]?\\.(?:inf|Inf|INF) 
    |\\.(?:nan|NaN|NAN))$''', re.X), 
    list(u'-.')) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:int', 
    re.compile(u'''^(?:[-+]?0b[0-1_]+ 
    |[-+]?[0-9]+ 
    |[-+]?0o?[0-7_]+ 
    |[-+]?(?:0|[1-9][0-9_]*) 
    |[-+]?0x[0-9a-fA-F_]+ 
    |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X), 
    list(u'-')) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:merge', 
    re.compile(u'^(?:<<)$'), 
    [u'<']) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:null', 
    re.compile(u'''^(?: ~ 
    |null|Null|NULL 
    |)$''', re.X), 
    [u'~', u'n', u'N', u'']) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:timestamp', 
    re.compile(u'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] 
    |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]? 
    (?:[Tt]|[ \\t]+)[0-9][0-9]? 
    :[0-9][0-9] :[0-9][0-9] (?:\\.[0-9]*)? 
    (?:[ \\t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X), 
    list(u'')) 

MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:value', 
    re.compile(u'^(?:=)$'), 
    [u'=']) 

# The following resolver is only for documentation purposes. It cannot work 
# because plain scalars cannot start with '!', '&', or '*'. 
MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:yaml', 
    re.compile(u'^(?:!|&|\\*)$'), 
    list(u'!&*')) 


class MySafeDumper(Emitter, Serializer, Repr, MyResolver): 
    def __init__(self, stream, 
       default_style=None, default_flow_style=None, 
       canonical=None, indent=None, width=None, 
       allow_unicode=None, line_break=None, 
       encoding=None, explicit_start=None, explicit_end=None, 
       version=None, tags=None): 
     Emitter.__init__(self, stream, canonical=canonical, 
         indent=indent, width=width, 
         allow_unicode=allow_unicode, line_break=line_break) 
     Serializer.__init__(self, encoding=encoding, 
          explicit_start=explicit_start, 
          explicit_end=explicit_end, 
          version=version, tags=tags) 
     Repr.__init__(self, default_style=default_style, 
           default_flow_style=default_flow_style) 
     MyResolver.__init__(self) 

print(yaml.dump(dict(sampe='Yes'), Dumper=MySafeDumper)) 

這給:

{sampe: Yes} 

如您在您的評論表示。如果您希望能夠將其作爲字符串加載,則應使用上面鏈接的答案中的構造函數代碼,兩個答案的解析器部分都是相同的。

+0

感謝您的明確解釋。構造函數和解析器工作正常。 –