D
D
Dmitry Prilepsky2021-08-27 14:40:53
Python
Dmitry Prilepsky, 2021-08-27 14:40:53

How to write normal lines and line breaks to .yml file using python?

I do automatic translation of one toy. I take lines from a yml file, translate and want to write back by analogy. Here is a piece of such a file:

l_english:
  bos_justice:0 "Justice"
  bos_justice_desc:0 "On October 10th 2077, less than a week before nuclear fire destroyed all that was and had been, Robert Maxson and his followers descended upon the West-Tek facility in hopes of finding resources and supplies. Instead, they found a freak show, a nightmarish realm of FEV and mad scientists. Robert was furious, and what he decided to do with these mad men would echo through the centuries."

  bos_glow:0 "The Glow"
  bos_glow_desc:0 "In 2134, after the formal formation of the Brotherhood of Steel, a splinter faction lead by Dennis Allen, who was among Maxson's original soldiers, requested permission to return to the West-Tek facility in search of technological artefacts. Understanding fully that this was the most dangerous mission ever presented to the council, he trusted his good reputation to garner him permission."

  bos_vipers:0 "The Vipers"
  bos_vipers_desc:0 "The War with the Vipers was a skirmish like any other, routine patrols engaging in small and one sided firefights with random raider gangs. But this time it was different, this time there were casualties. Maxson himself died hours after the engagement, writing in pain as radscorpion venom claimed him. This painful and horrific death lead to outrage among the Brotherhood of Steel."

  bos_oldamerica:0 "Old America"
  bos_oldamerica_desc:0 "The Brotherhood was not the only pre-war military outfit to survive the end of America, and that became overwhelmingly apparent with the rise of the Enclave. After years of passive observation and subtle manoeuvring, the Brotherhood of Steel joined the NCR in the final battle at the Navarro Outpost. This was in many ways, the opening of a new Chapter for the Brotherhood of Steel."

 # Underground Hangars sub-tree

  bos_hangars:0 "Underground Hangars"
  bos_hangars_desc:0 "Some Pre-War Military Bunkers come equipped with underground hangars, allowing Vertibird storage in relative safety and obscurity. Many of these facilities also come with facilities for repair and ample fuel storage. Bringing them back online is necessary for power protection."

# Underground Hangars sub-treeI don't understand how can I make type strings and empty strings using the yaml library in python . Also my code (here it is):
import yaml

to_yaml = {'l_english': [{'bos_justice': '0 text'}, {'bos_justice_desc': '0 text'}]}

with open('sw_templates.yaml', 'w') as f:
    yaml.dump(to_yaml, f, default_flow_style=False)


Makes an extra space between : and 0 and puts dashes before lines.
l_english:
- bos_justice: 0 text
- bos_justice_desc: 0 text

I can't even find examples of building such a yml file. Please tell me how can I do this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-08-27
@HartX

Lists by default turn into yaml "with dashes", and dictionaries - without dashes.
As for # - in the yaml syntax standard it means comments. I read from the link that it is difficult to stuff comments into pyyaml, and it's easier to use another library - ruamel.yaml
As for the spaces after the colon - I'm afraid, except for post-processing, you can't get rid of them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question