Advertisement
Guest User

Untitled

a guest
Apr 7th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. Code:
  2. import yaml
  3. import os
  4. import re
  5.  
  6. with open("test.yaml", 'r') as stream:
  7. try:
  8. loaded=stream.read()
  9. except yaml.YAMLError as exc:
  10. print(exc)
  11.  
  12. with open("modified.yaml", 'w') as stream:
  13. try:
  14. yaml.dump(re.sub(r'anotherName: [^\n]*', 'anotherName: somethingChanged', loaded), stream)
  15. except yaml.YAMLError as exc:
  16. print(exc)
  17.  
  18. OUTPUT:
  19.  
  20. cat modified.yaml
  21. "---\nkind: test\nscope: not far\nspec:\n test1:\n options:\n - name: test.com\n\
  22. \ anotherName: somethingChanged\n empty: []\n groupOne: []\n \
  23. \ emptyList:\n firstList: []\n secondList: []"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement