Modify

Ticket #167 (closed defect: worksforme)

Opened 4 years ago

Last modified 4 years ago

Please help saving dict to yaml

Reported by: anonymous Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description

Hi, is there any way of saving dict to a file in yaml format? I'm using:
----------------------------------------
#!/usr/bin/python
import yaml
y = """
    root:
         - subject1:
            desc: msg1
         - subject2:
            desc: msg2
"""
settings=yaml.load(y)

settings['root'][0]['subject1']['desc'] = "new msg"
print settings

f = open("temp", "w")
f.write(str(settings))
----------------------------------------
but failing miserably, contents of the temp file:
{'root': [{'subject1': {'desc': 'new msg'}}, {'subject2': {'desc': 'msg2'}}]}

expected result:
    root:
         - subject1:
            desc: new msg
         - subject2:
            desc: msg2

Is there anything like yaml.save? Any help greatly appreciated!

Regards,
Tomas

Attachments

Change History

comment:1 Changed 4 years ago by xi

  • Status changed from new to closed
  • Resolution set to worksforme

Use yaml.dump:

f = open("temp", "w")
f.write(yaml.dump(settings, default_flow_style=False))

comment:2 Changed 4 years ago by anonymous

Thanks!

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.