代码之家  ›  专栏  ›  技术社区  ›  tafazzi87

使用Python转储具有正确多行且无换行符的yaml文件

  •  0
  • tafazzi87  · 技术社区  · 8 年前

    我需要在yaml中重新创建该文件:

    - account_type: 3
      active: true
      addresses: 
      - address: !!python/unicode 'firstname.lastname@email.com'
        enabled: true
        username: !!python/unicode 'firstname.lastname@email.com'
      email: !!python/unicode 'firstname.lastname@email.com'
      firstname: firstname
      high_score: 0.0
      lastname: lastname
      lists: []
      local: false
      low_score: 0.0
      password1: !!python/unicode 'Deef2fht'
      password2: !!python/unicode 'Deef2fht'
      send_report: true
      signatures: []
      spam_checks: true
      timezone: !!python/unicode 'Europe/Rome'
      username: !!python/unicode 'firstname.lastname@email.com'
    

    所以我创建了这段代码:

    import yaml
    import random
    import string
    import sys
    email = sys.argv[1]
    dominio = email.split("@")
    nome = (dominio[0].split("."))[0]
    cognome = (dominio[0].split("."))[1]
    password = random = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)])
    document = """
      account_type: 3
      active: true
      addresses: 
        address: '"""+email+"""'
        enabled: true
        username: '"""+email+"""'
        email: '"""+email+"""'
      firstname: """+nome+"""
      high_score: 0.0
      lastname: """+cognome+"""
      lists: []
      local: false
      low_score: 0.0
      password1: '"""+password+"""'
      password2: '"""+password+"""'
      send_report: true
      signatures: []
      spam_checks: true
      timezone: 'Europe/Rome'
      username: '"""+email+"""'
    """
    yaml.safe_dump(document, open("output.yaml", "w"), default_flow_style=False)
    

    但是输出只在一行上,对于文档变量上的每一个换行,我都有/n个字符。 有没有一种方法可以做到这一点,而不必在不同的变量上打印每一行?

    1 回复  |  直到 8 年前
        1
  •  2
  •   aylr    8 年前

    不要使用字符串串联,而是构建一个表示yml对象的字典。然后使用 yaml.dump() 将其写回磁盘。了解yaml库工作原理的一种简单方法是将现有文件加载到变量中,更改一些值并将其写回磁盘。

    1. 创建一个foo。yml文件和目标yaml:

      - account_type: 3
        active: true
        addresses: 
        - address: !!python/unicode 'firstname.lastname@email.com'
          enabled: true
          username: !!python/unicode 'firstname.lastname@email.com'
        email: !!python/unicode 'firstname.lastname@email.com'
        firstname: firstname
        high_score: 0.0
        lastname: lastname
        lists: []
        local: false
        low_score: 0.0
        password1: !!python/unicode 'Deef2fht'
        password2: !!python/unicode 'Deef2fht'
        send_report: true
        signatures: []
        spam_checks: true
        timezone: !!python/unicode 'Europe/Rome'
        username: !!python/unicode 'firstname.lastname@email.com'
      
    2. 将文件读入变量,研究其结构,更改用户名并将新的yml文件保存回磁盘。

      import yaml
      x = {}
      with open('foo.yml', 'r') as my_file:
          x = yaml.load(my_file)
      
      x[0]['username'] = 'different_user@domain.com'
      
      with open('new.yml', 'w') as new_file:
          yaml.dump(x, new_file)