【问题标题】:escape characters and strings转义字符和字符串
【发布时间】:2016-04-15 13:44:15
【问题描述】:

我有一个函数:

def set_localrepo(self):

    stream = open("file1.yml", "r")
    results = yaml.load(stream)
    run_cmd = results["parameters"]["tag"]["properties"]
    config_list = ( 'sleep 200', '[sh, -xc, \"echo test\'  test\' >> /etc/hosts\"]')
    for i in range(len(config_list)):
        run_cmd.append(config_list[i])
    stream.close()
    with open("f2.yml", "w") as yaml_file:
        yaml_file.write(yaml.dump(results, default_flow_style=False, allow_unicode=True))
    yaml_file.close()    

在这个文件中,我有一个 file1.yml 框架,并从那里处理列表中的内容并将其写入 f2.yml。

预期输出应如下所示:

        properties:
        - sleep 200 
        - [sh, -xc, "echo $repo_server'  repo-server' >> /etc/hosts"]

但它看起来像这样:

        properties:
        - sleep 200 
        - '[sh, -xc, "echo $repo_server''  repo-server'' >> /etc/hosts"]'

我已经尝试了双\、单\等的多种组合,但它可以按我的意愿工作。

请告知可以采取什么措施来解决此问题。我怀疑它与 YAML 转储实用程序和转义字符组合有关。

感谢期待!

【问题讨论】:

    标签: python yaml unicode-escapes


    【解决方案1】:

    我不是 YAML 专家,但我认为这是预期的行为。

    您的程序的复制,使用yaml.load 重新加载 YAML 文件表明它已正确地重建字符串,正如预期的那样:

    import yaml
    
    config_list = ( 'sleep 200', '[sh, -xc, "echo $test\'  test\' >> /etc/hosts"]')
    results = {'properties': []}
    run_cmd = results['properties']
    for i in range(len(config_list)):
        run_cmd.append(config_list[i])
    
    with open("f2.yml", "w") as yaml_file:
        yaml_file.write(yaml.dump(results, default_flow_style=False, allow_unicode=True))
    
    yaml_file.close()
    
    yaml_file2 = open('f2.yml', 'r')
    data = yaml_file2.read()
    print(yaml.load(data))
    

    这会产生一个输出

    {'properties': ['sleep 200', '[sh, -xc, "echo $test\'  test\' >> /etc/hosts"]']}
    

    这正是您所期望的。在外部使用单引号并在内部使用'' 必须是 YAML 对列表项中的单引号进行转义的方式。

    【讨论】:

      【解决方案2】:

      根据您的预期输出,第二项不是字符串而是列表。因此,为了在 Python 中正确序列化,它也必须是一个列表(或元组)。这意味着您的 config_list 应如下所示:

      ( 'sleep 200', ['sh', '-xc', '"echo test\'  test\' >> /etc/hosts"'] )
      

      更改后,输出将是:

      parameters:
        tags:
          properties:
          - sleep 200
          - - sh
            - -xc
            - '"echo test''  test'' >> /etc/hosts"'
      

      因为你禁用了default_flow_style,所以你有一个奇怪的嵌套列表,它相当于:

      parameters:
        tags:
          properties:
          - sleep 200
          - [sh, -xc, '"echo test''  test'' >> /etc/hosts"']
      

      如果您担心您的单引号在 YAML means one single quote only 中作为双单引号 在单引号字符串中 是正确的。

      额外。不要使用:

      for i in range(len(config_list)):
        item = config_list[i]
        # ...
      

      改为使用更简单的迭代模式:

      for item in config_list:
        # ...
      

      【讨论】:

      • 但是你如何解释这个:( 'sleep 200', '#Comment') 被打印为- sleep 200 - '#Comment'
      • 来自我在答案中引用的链接:必须引用包含以下任何字符的字符串。虽然可以使用双引号,但对于这些字符,使用单引号更方便,这样可以避免转义任何反斜杠 \: :, {, }, [, ], ,, &, *, #, ?, |, -、、=、!、%、@、`
      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      相关资源
      最近更新 更多