tafazzi87
tafazzi87

Reputation: 449

dump yaml file with correct multiline without newline character with Python

i need to recreate that file in yaml :

- account_type: 3
  active: true
  addresses: 
  - address: !!python/unicode '[email protected]'
    enabled: true
    username: !!python/unicode '[email protected]'
  email: !!python/unicode '[email protected]'
  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 '[email protected]'

so i create this piece of code :

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)

but the output is only on a line and for every newline on my document variable i've /n character. There is a way to do that without print every line on a different variable?

Upvotes: 0

Views: 2506

Answers (1)

aylr
aylr

Reputation: 369

Rather than using string concatenation, build a dictionary that represents your yml object. Then use yaml.dump() to write that back to disk. An easy way to understand how the yaml library works is to load an existing file into a variable, change some values and write it back to disk.

  1. Create a foo.yml file with your goal yaml:

    - account_type: 3
      active: true
      addresses: 
      - address: !!python/unicode '[email protected]'
        enabled: true
        username: !!python/unicode '[email protected]'
      email: !!python/unicode '[email protected]'
      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 '[email protected]'
    
  2. Read the file into a variable, explore it's structure, change the username and save a new yml file back to disk.

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

Upvotes: 2

Related Questions