简体   繁体   中英

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 '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'

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?

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 '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. 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'] = 'different_user@domain.com' with open('new.yml', 'w') as new_file: yaml.dump(x, new_file) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM