python-3.x PyYAML在多行中转储长字符串

hec6srdp  于 2023-07-01  发布在  Python
关注(0)|答案(1)|浏览(127)

pyYAML在多行中转储一个长字符串。我想要输出像这样-

include:
  - interface Vlan6[\s]+vrrpv3 1 address-family ipv4[\s]+address 5.2.1.1 primary[\s]+interface Vlan7[\s]+vrrpv3 1 address-family ipv4[\s]+address 5.2.1.2

但是,文件中的转储输出如下所示-

include:
  - interface Vlan6[\s]+vrrpv3 1 address-family ipv4[\s]+address 5.2.1.1
    primary[\s]+interface Vlan7[\s]+vrrpv3 1 address-family ipv4[\s]+address
    5.2.1.2

甚至还遇到了一个关于使用SafeDumper解决这个问题的链接,但我使用另一个自定义转储程序将数据转储到一个文件中,用于不同的用例(修复缩进)。这里是代码-

# MyDumper Class: To correct the indentation of list items
# https://stackoverflow.com/questions/25108581/python-yaml-dump-bad-indentation
class MyDumper(yaml.Dumper):

    def increase_indent(self, flow=False, indentless=False):
        return super(MyDumper, self).increase_indent(flow, False)

# Use str for python3 and unicode for lower versions
# https://stackoverflow.com/questions/6432605/any-yaml-libraries-in-python-that-support-dumping-of-long-strings-as-block-liter   
class literal_unicode(str): pass

def literal_unicode_representer(dumper, data):
    return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')

yaml.add_representer(literal_unicode, literal_unicode_representer)

# save_verify_target -> configure -> commands
key1 = 'interface Vlan6\nvrrpv3 1 address-family ipv4\naddress 5.2.1.1 primary\ninterface vlan 7\nvrrpv3 1 address-family ipv4\naddress 5.2.1.2\n'
# save_verify_target -> execute -> commands
key2 = 'show file vrrp.cfg'

verify_target_str = 'interface Vlan6[\s]+vrrpv3 1 address-family ipv4[\s]+address 5.2.1.1 primary[\s]+interface Vlan7[\s]+vrrpv3 1 address-family ipv4[\s]+address 5.2.1.2'

dict_file = {
    'extends': 'common_trigger.yaml',
    'setup_device': {
        'description': 'Setup device',
        'source': {
            'pkg': 'libs.sdk'
        },
        'groups': ['all', 'custom']
    },
    'Test01': {
        'description': 'ID:TC01',
        'source': {
            'pkg': 'libs.sdk'
        },
        'groups': ['all', 'custom'],
        'test_sections': [
            '%{ triggers.pre_config }',
            {'save_verify_target': [
                {'configure': {
                    'custom_start_step_message': 'Save target config to a file',
                    'device': 'uut1',
                    'command': literal_unicode(key1)
                }},
                {'execute': {
                    'custom_start_step_message': 'Verify the target file contents',
                    'device': 'uut1',
                    'command': literal_unicode(key2),
                    'include': [verify_target_str]
                }}
            ]}
        ]
    }
}

with open(r'check.yaml', 'w') as file:
    documents = yaml.dump(dict_file, file, sort_keys=False, Dumper=MyDumper, default_flow_style=False)

这里'verify_target_str'是key 'include'的值,是一个很长的字符串,我希望它在最终的YAML文件中只出现一行,即check.yaml.
有人能帮帮我吗?

42fyovps

42fyovps1#

PyYAML的dump有一个参数width,默认值(IIRC)为78,这就是长行被 Package 的地方。只要传递一个足够大的值,就不再需要 Package 了。
当然,您希望在一行中包含这些内容与正确的YAML解析器无关。这些,甚至PyYAML本身,将把 Package 的标量加载到原始的Python字符串中。

相关问题