Home > other >  Python for yaml files to add and delete problem ~ ~ ~ ~
Python for yaml files to add and delete problem ~ ~ ~ ~

Time:10-25

Processing duplicate structure yaml files, demand is to enter the operation parameters and to find changes the value of the corresponding example: sys. Argv [1]="a/b/[0]/.../name ", enter on behalf of each level, normal query is that A=yaml. Load [" filename "], value=HTTP://https://bbs.csdn.net/topics/A/" A "(" b") [0]... (" name "), while due to parameter uncertainty, a query to the accurate place have no idea, consult everybody a great god have what good method?

CodePudding user response:

Yaml file is as follows:

Name: Tom Smith
Age: 37
A1:
Spouse:
Name: Jane Smith1
Age: 25
Children:
- name: Jimmy Smith2
Age: 15
- name1: Jenny Smith3
Age1:12
A2:
Spouse:
Name: Jane Smith4
Age: 25
Children1:
- name: Jimmy Smith5
Age: 15
- name1: Jenny example
Age1:12

A2:
Spouse:
Name: Jane Smith
Age: 25
Children:
- name: Jimmy Smith
Age: 15
- name1: Jenny Smith
Age1:12

 import yaml 
Name='a1/children/[1]/a2/children1/[1]/name1'
Path=r "C: \ Users \... \ work_test \ yaml yaml
"
Yaml_obj=yaml. Load (open (path, 'r'), the Loader=yaml. FullLoader)

Keys=name. The split ('/')
B=yaml_obj [keys [0]]

For key keys in [1] :
If the key. The startswith (" [") :
Key=int (key [1, 1])
[key]
b=bPrint (" {}, {} ". The format (key, b))
The else:
[key]
b=bPrint (" {}, {} ". The format (key, b))
Print b
  • Related