How to edit a YAML file in python repeatedly using a variable without defining a specific value for the key?












0















This is my example YAML file,



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


I want to read this yaml file from a particular directory, edit it and then save the edited text on the same yaml file. I got this code from a link somewhere as follows:



import sys
from ruamel.yaml import YAML
inp_fo = open("C:/<Users>/Master
1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml").read()
#Read the YAML File

yaml = YAML()
code = yaml.load(inp_fo)
sys.stdout.write('n')
code.insert(1, 'sensor', 'None', comment="new key")
inp_fo2 = open("inp.yaml","w") #Open the file for Write
yaml.dump(code, inp_fo2) ##Write to file with new parameter
inp_fo2.close() ## close the file


When I execute it I get output as follows:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha: None


But instead I need:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


How to code this?



I entered value for 'Tabitha' as None because code.insert was not getting executed if I don't insert a value for it.



Now, I want to edit this yaml file repeatedly so I want to define a variable for each key value, so everytime once the variable value changes the code will be executed and new key values will be inserted in the existing yaml file. I don't want to delete the earlier contents of YAML file just need to go on adding new contents (key & value) (content will be employee details like name, job, skills).



I am completely new to this coding language so if I am wrong please correct me. Also can anyone explain me what is the use of indent, sequence & mapping in ruamel.yaml and how can I use them in code?



I am a learner and the above example is a random yaml file just an example to learn ruamel.yaml.



Let me explain you what I want exactly,
The following content will remain static.



 # Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1


But the key values (employee details) are going to change. Example I want:
Martin:
name: Martin Devloper
job: Developer
skills:
- python



"Martin" should be a "variableP"
name: "variableQ"
job: "variableR"
skills:
- "variableS"


similarly if another employee adds his details the code will automatically add another employee details in those variables.
because according to your code, I will have to repeatedly add



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


again and again for other employee data which I don't want, I just want a code where I could define a variable whose value will keep on changing and will edit the same yaml file repeatedly and do not delete the earlier employee details.
There is no limit for list of employees to be get added so I don't want to edit the code again and again.



I basically know C and arduino programming language so looking for a way to define variables in a code which will make my work easy.










share|improve this question

























  • Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

    – Cubic
    Nov 17 '18 at 14:38













  • Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

    – Anthon
    Nov 17 '18 at 20:02











  • Yes the values for the variable will come from user input basically from a local website.

    – MEHUL SOLANKI
    Nov 17 '18 at 20:10











  • What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

    – Anthon
    Nov 17 '18 at 20:23











  • I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

    – MEHUL SOLANKI
    Nov 18 '18 at 7:33


















0















This is my example YAML file,



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


I want to read this yaml file from a particular directory, edit it and then save the edited text on the same yaml file. I got this code from a link somewhere as follows:



import sys
from ruamel.yaml import YAML
inp_fo = open("C:/<Users>/Master
1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml").read()
#Read the YAML File

yaml = YAML()
code = yaml.load(inp_fo)
sys.stdout.write('n')
code.insert(1, 'sensor', 'None', comment="new key")
inp_fo2 = open("inp.yaml","w") #Open the file for Write
yaml.dump(code, inp_fo2) ##Write to file with new parameter
inp_fo2.close() ## close the file


When I execute it I get output as follows:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha: None


But instead I need:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


How to code this?



I entered value for 'Tabitha' as None because code.insert was not getting executed if I don't insert a value for it.



Now, I want to edit this yaml file repeatedly so I want to define a variable for each key value, so everytime once the variable value changes the code will be executed and new key values will be inserted in the existing yaml file. I don't want to delete the earlier contents of YAML file just need to go on adding new contents (key & value) (content will be employee details like name, job, skills).



I am completely new to this coding language so if I am wrong please correct me. Also can anyone explain me what is the use of indent, sequence & mapping in ruamel.yaml and how can I use them in code?



I am a learner and the above example is a random yaml file just an example to learn ruamel.yaml.



Let me explain you what I want exactly,
The following content will remain static.



 # Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1


But the key values (employee details) are going to change. Example I want:
Martin:
name: Martin Devloper
job: Developer
skills:
- python



"Martin" should be a "variableP"
name: "variableQ"
job: "variableR"
skills:
- "variableS"


similarly if another employee adds his details the code will automatically add another employee details in those variables.
because according to your code, I will have to repeatedly add



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


again and again for other employee data which I don't want, I just want a code where I could define a variable whose value will keep on changing and will edit the same yaml file repeatedly and do not delete the earlier employee details.
There is no limit for list of employees to be get added so I don't want to edit the code again and again.



I basically know C and arduino programming language so looking for a way to define variables in a code which will make my work easy.










share|improve this question

























  • Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

    – Cubic
    Nov 17 '18 at 14:38













  • Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

    – Anthon
    Nov 17 '18 at 20:02











  • Yes the values for the variable will come from user input basically from a local website.

    – MEHUL SOLANKI
    Nov 17 '18 at 20:10











  • What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

    – Anthon
    Nov 17 '18 at 20:23











  • I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

    – MEHUL SOLANKI
    Nov 18 '18 at 7:33
















0












0








0


1






This is my example YAML file,



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


I want to read this yaml file from a particular directory, edit it and then save the edited text on the same yaml file. I got this code from a link somewhere as follows:



import sys
from ruamel.yaml import YAML
inp_fo = open("C:/<Users>/Master
1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml").read()
#Read the YAML File

yaml = YAML()
code = yaml.load(inp_fo)
sys.stdout.write('n')
code.insert(1, 'sensor', 'None', comment="new key")
inp_fo2 = open("inp.yaml","w") #Open the file for Write
yaml.dump(code, inp_fo2) ##Write to file with new parameter
inp_fo2.close() ## close the file


When I execute it I get output as follows:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha: None


But instead I need:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


How to code this?



I entered value for 'Tabitha' as None because code.insert was not getting executed if I don't insert a value for it.



Now, I want to edit this yaml file repeatedly so I want to define a variable for each key value, so everytime once the variable value changes the code will be executed and new key values will be inserted in the existing yaml file. I don't want to delete the earlier contents of YAML file just need to go on adding new contents (key & value) (content will be employee details like name, job, skills).



I am completely new to this coding language so if I am wrong please correct me. Also can anyone explain me what is the use of indent, sequence & mapping in ruamel.yaml and how can I use them in code?



I am a learner and the above example is a random yaml file just an example to learn ruamel.yaml.



Let me explain you what I want exactly,
The following content will remain static.



 # Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1


But the key values (employee details) are going to change. Example I want:
Martin:
name: Martin Devloper
job: Developer
skills:
- python



"Martin" should be a "variableP"
name: "variableQ"
job: "variableR"
skills:
- "variableS"


similarly if another employee adds his details the code will automatically add another employee details in those variables.
because according to your code, I will have to repeatedly add



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


again and again for other employee data which I don't want, I just want a code where I could define a variable whose value will keep on changing and will edit the same yaml file repeatedly and do not delete the earlier employee details.
There is no limit for list of employees to be get added so I don't want to edit the code again and again.



I basically know C and arduino programming language so looking for a way to define variables in a code which will make my work easy.










share|improve this question
















This is my example YAML file,



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


I want to read this yaml file from a particular directory, edit it and then save the edited text on the same yaml file. I got this code from a link somewhere as follows:



import sys
from ruamel.yaml import YAML
inp_fo = open("C:/<Users>/Master
1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml").read()
#Read the YAML File

yaml = YAML()
code = yaml.load(inp_fo)
sys.stdout.write('n')
code.insert(1, 'sensor', 'None', comment="new key")
inp_fo2 = open("inp.yaml","w") #Open the file for Write
yaml.dump(code, inp_fo2) ##Write to file with new parameter
inp_fo2.close() ## close the file


When I execute it I get output as follows:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha: None


But instead I need:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


How to code this?



I entered value for 'Tabitha' as None because code.insert was not getting executed if I don't insert a value for it.



Now, I want to edit this yaml file repeatedly so I want to define a variable for each key value, so everytime once the variable value changes the code will be executed and new key values will be inserted in the existing yaml file. I don't want to delete the earlier contents of YAML file just need to go on adding new contents (key & value) (content will be employee details like name, job, skills).



I am completely new to this coding language so if I am wrong please correct me. Also can anyone explain me what is the use of indent, sequence & mapping in ruamel.yaml and how can I use them in code?



I am a learner and the above example is a random yaml file just an example to learn ruamel.yaml.



Let me explain you what I want exactly,
The following content will remain static.



 # Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1


But the key values (employee details) are going to change. Example I want:
Martin:
name: Martin Devloper
job: Developer
skills:
- python



"Martin" should be a "variableP"
name: "variableQ"
job: "variableR"
skills:
- "variableS"


similarly if another employee adds his details the code will automatically add another employee details in those variables.
because according to your code, I will have to repeatedly add



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


again and again for other employee data which I don't want, I just want a code where I could define a variable whose value will keep on changing and will edit the same yaml file repeatedly and do not delete the earlier employee details.
There is no limit for list of employees to be get added so I don't want to edit the code again and again.



I basically know C and arduino programming language so looking for a way to define variables in a code which will make my work easy.







python yaml pyyaml ruamel.yaml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 20:20









Anthon

29.7k1693146




29.7k1693146










asked Nov 17 '18 at 13:49









MEHUL SOLANKIMEHUL SOLANKI

174




174













  • Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

    – Cubic
    Nov 17 '18 at 14:38













  • Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

    – Anthon
    Nov 17 '18 at 20:02











  • Yes the values for the variable will come from user input basically from a local website.

    – MEHUL SOLANKI
    Nov 17 '18 at 20:10











  • What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

    – Anthon
    Nov 17 '18 at 20:23











  • I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

    – MEHUL SOLANKI
    Nov 18 '18 at 7:33





















  • Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

    – Cubic
    Nov 17 '18 at 14:38













  • Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

    – Anthon
    Nov 17 '18 at 20:02











  • Yes the values for the variable will come from user input basically from a local website.

    – MEHUL SOLANKI
    Nov 17 '18 at 20:10











  • What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

    – Anthon
    Nov 17 '18 at 20:23











  • I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

    – MEHUL SOLANKI
    Nov 18 '18 at 7:33



















Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

– Cubic
Nov 17 '18 at 14:38







Worth pointing out that you're essentially using this YAML file as a crappy database. Unless you really care about having life updates on a human readable text file at all times, you should probably look into something like sqlite instead which is built for this sort of usage.

– Cubic
Nov 17 '18 at 14:38















Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

– Anthon
Nov 17 '18 at 20:02





Please explain in detail where the values for your variables come from (from another YAML file? From user input? ,etc).

– Anthon
Nov 17 '18 at 20:02













Yes the values for the variable will come from user input basically from a local website.

– MEHUL SOLANKI
Nov 17 '18 at 20:10





Yes the values for the variable will come from user input basically from a local website.

– MEHUL SOLANKI
Nov 17 '18 at 20:10













What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

– Anthon
Nov 17 '18 at 20:23





What is the file format that the local website writes? YAML files? What is the exact format of what you get from the website? I hope you are not trying to make your python the back-end (without experienced help).

– Anthon
Nov 17 '18 at 20:23













I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

– MEHUL SOLANKI
Nov 18 '18 at 7:33







I will be getting data in Jason format from the website, I guess is that what you are asking? Then I want to read that Jason data and add it to the variable in this code. Yes the python code will be running in back-end.

– MEHUL SOLANKI
Nov 18 '18 at 7:33














2 Answers
2






active

oldest

votes


















0














The following assumes you are using Python3 (and you should if you are
learning Python), because it uses pathlib that is in the standard
library for Python3. There is a
pathlib2 package that provides that
functionality for Python2.



The example code that you present does a few strange things:




  • it is not necessary to read in the content of a file
    (open(...).read()) and then hand that to the .load() method, you
    can directly pass in the file pointer (i.e. the result from just
    doing open(), or you can use the Path object and hand this to
    .load() without even opening the file (as shown below).


  • it is unclear why a newline is written to sys.stdout, that really
    has no function in this snippet


  • the call code.insert(1, 'sensor', 'None', comment="new key") does put 'sensor' as
    a new key, in position 1 of the dictionary like object code, with the value None
    and the end-of-line comment "new key". (Position counting starts at 0.



If you had really ran that code you would get as output:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
sensor: None # new key
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


At the root of your input file is a mapping with keys Developer,
Support and Martin. The values associate with a key can again be a
mapping (as is the case for all your root-level keys), but they can
also be a sequence (as is the value for skills, indicated by dash
(-) or scalar values (string, number, etc). The root of a YAML
document can be a scalar (which is then the only object in the
document), or a sequence. And a sequence has elements that can be
scalars (as your final line python), but those elements can also be
mappings or other sequences.



Mappings are loaded as Python dicts, sequences are loaded as Python
lists. In order to preserve comments (and other information),
ruamel.yaml will create dict- resp. list-like objects when used as
in the example. You would not be able to call code.insert() on a regular
Python dict but you can do so on what you got back (from YAML().load())



If you just want to add a key-value pair at the end of a dict, you don't need to
use .insert, you can just do:



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


If you need to move something to the end of the YAML file, you actually cannot
just update the value using e.g.



code['Support'] = dict(Department='General', Manager='KLM', Floor='1st', Lab=1)


this will get you the key/value in the old position. You would have to do



del code['Support']


first to make it forget this old position.





As indicated ruamel.yaml supports multiple ways on how to read and
write to a file. The with statement as Moralous answer introduced
is already better than explicit opening, reading and closing. You can also use
pathlib.Path like objects with the .load() and .dump() methods:



from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

yaml = YAML()
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code, opath)


which gives:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


This is almost what you want, but the default indentation of the YAML output
is 2 positions for both the dict (as you want it to be) and for the
sequence (it is counted to the beginning of your element, not to the
dash).



To get what you want, you need to specify an indent of four for the sequence, and
an offset of two for the dash withing those for spaces. You can do so by inserting,
after the yaml = YAML() line, a line with:



yaml.indent(sequence=4, offset=2)




You can also use your YAML instance itself in a with statement:



import sys
from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

with YAML(output=opath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code)


That will also give you exactly you want:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


You tagged your question with PyYAML as well. Please note that with that package
you cannot do the above: your comments in the YAML file will be lost, and
you don't have the kind of fine control over indentation to get the result you
wanted.






share|improve this answer


























  • Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

    – MEHUL SOLANKI
    Nov 17 '18 at 19:01













  • I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

    – MEHUL SOLANKI
    Nov 17 '18 at 19:30











  • Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

    – Anthon
    Nov 17 '18 at 19:59



















0














The YAML library you're using converts between YAML formatting and Python dictionaries. Therefore you could simple edit the dictionary to add or change a value in the YAML file. For example:



import sys
from ruamel.yaml import YAML
filename = "C:/<Users>/Master/1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml"

yaml = YAML()
with open(filename) as inp_fo:
code = yaml.load(inp_fo)

code["Tabitha"] = {
"name":"Tabitha Bitman",
"job":"Developer",
"skills":["Python"]
}

with open(filename, "w") as file:
yaml.dump(code, file) ##Write to file with new parameter



There is no need to close the files as they were opened in "with"
statements




This will update Tabitha's details in the YAML file, assuming that the rest of your code is correct.



You can also parse a variable into the key:value lookup, replacing code["Tabitha"] with code[variable_name].



If you are working with YAML (or JSON) files I also highly recommend looking into and understanding dictionaries in Python.






share|improve this answer


























  • Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

    – MEHUL SOLANKI
    Nov 17 '18 at 15:34













  • @MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

    – Moralous
    Nov 17 '18 at 16:25






  • 1





    @moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

    – Anthon
    Nov 17 '18 at 17:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351840%2fhow-to-edit-a-yaml-file-in-python-repeatedly-using-a-variable-without-defining-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The following assumes you are using Python3 (and you should if you are
learning Python), because it uses pathlib that is in the standard
library for Python3. There is a
pathlib2 package that provides that
functionality for Python2.



The example code that you present does a few strange things:




  • it is not necessary to read in the content of a file
    (open(...).read()) and then hand that to the .load() method, you
    can directly pass in the file pointer (i.e. the result from just
    doing open(), or you can use the Path object and hand this to
    .load() without even opening the file (as shown below).


  • it is unclear why a newline is written to sys.stdout, that really
    has no function in this snippet


  • the call code.insert(1, 'sensor', 'None', comment="new key") does put 'sensor' as
    a new key, in position 1 of the dictionary like object code, with the value None
    and the end-of-line comment "new key". (Position counting starts at 0.



If you had really ran that code you would get as output:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
sensor: None # new key
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


At the root of your input file is a mapping with keys Developer,
Support and Martin. The values associate with a key can again be a
mapping (as is the case for all your root-level keys), but they can
also be a sequence (as is the value for skills, indicated by dash
(-) or scalar values (string, number, etc). The root of a YAML
document can be a scalar (which is then the only object in the
document), or a sequence. And a sequence has elements that can be
scalars (as your final line python), but those elements can also be
mappings or other sequences.



Mappings are loaded as Python dicts, sequences are loaded as Python
lists. In order to preserve comments (and other information),
ruamel.yaml will create dict- resp. list-like objects when used as
in the example. You would not be able to call code.insert() on a regular
Python dict but you can do so on what you got back (from YAML().load())



If you just want to add a key-value pair at the end of a dict, you don't need to
use .insert, you can just do:



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


If you need to move something to the end of the YAML file, you actually cannot
just update the value using e.g.



code['Support'] = dict(Department='General', Manager='KLM', Floor='1st', Lab=1)


this will get you the key/value in the old position. You would have to do



del code['Support']


first to make it forget this old position.





As indicated ruamel.yaml supports multiple ways on how to read and
write to a file. The with statement as Moralous answer introduced
is already better than explicit opening, reading and closing. You can also use
pathlib.Path like objects with the .load() and .dump() methods:



from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

yaml = YAML()
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code, opath)


which gives:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


This is almost what you want, but the default indentation of the YAML output
is 2 positions for both the dict (as you want it to be) and for the
sequence (it is counted to the beginning of your element, not to the
dash).



To get what you want, you need to specify an indent of four for the sequence, and
an offset of two for the dash withing those for spaces. You can do so by inserting,
after the yaml = YAML() line, a line with:



yaml.indent(sequence=4, offset=2)




You can also use your YAML instance itself in a with statement:



import sys
from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

with YAML(output=opath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code)


That will also give you exactly you want:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


You tagged your question with PyYAML as well. Please note that with that package
you cannot do the above: your comments in the YAML file will be lost, and
you don't have the kind of fine control over indentation to get the result you
wanted.






share|improve this answer


























  • Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

    – MEHUL SOLANKI
    Nov 17 '18 at 19:01













  • I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

    – MEHUL SOLANKI
    Nov 17 '18 at 19:30











  • Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

    – Anthon
    Nov 17 '18 at 19:59
















0














The following assumes you are using Python3 (and you should if you are
learning Python), because it uses pathlib that is in the standard
library for Python3. There is a
pathlib2 package that provides that
functionality for Python2.



The example code that you present does a few strange things:




  • it is not necessary to read in the content of a file
    (open(...).read()) and then hand that to the .load() method, you
    can directly pass in the file pointer (i.e. the result from just
    doing open(), or you can use the Path object and hand this to
    .load() without even opening the file (as shown below).


  • it is unclear why a newline is written to sys.stdout, that really
    has no function in this snippet


  • the call code.insert(1, 'sensor', 'None', comment="new key") does put 'sensor' as
    a new key, in position 1 of the dictionary like object code, with the value None
    and the end-of-line comment "new key". (Position counting starts at 0.



If you had really ran that code you would get as output:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
sensor: None # new key
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


At the root of your input file is a mapping with keys Developer,
Support and Martin. The values associate with a key can again be a
mapping (as is the case for all your root-level keys), but they can
also be a sequence (as is the value for skills, indicated by dash
(-) or scalar values (string, number, etc). The root of a YAML
document can be a scalar (which is then the only object in the
document), or a sequence. And a sequence has elements that can be
scalars (as your final line python), but those elements can also be
mappings or other sequences.



Mappings are loaded as Python dicts, sequences are loaded as Python
lists. In order to preserve comments (and other information),
ruamel.yaml will create dict- resp. list-like objects when used as
in the example. You would not be able to call code.insert() on a regular
Python dict but you can do so on what you got back (from YAML().load())



If you just want to add a key-value pair at the end of a dict, you don't need to
use .insert, you can just do:



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


If you need to move something to the end of the YAML file, you actually cannot
just update the value using e.g.



code['Support'] = dict(Department='General', Manager='KLM', Floor='1st', Lab=1)


this will get you the key/value in the old position. You would have to do



del code['Support']


first to make it forget this old position.





As indicated ruamel.yaml supports multiple ways on how to read and
write to a file. The with statement as Moralous answer introduced
is already better than explicit opening, reading and closing. You can also use
pathlib.Path like objects with the .load() and .dump() methods:



from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

yaml = YAML()
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code, opath)


which gives:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


This is almost what you want, but the default indentation of the YAML output
is 2 positions for both the dict (as you want it to be) and for the
sequence (it is counted to the beginning of your element, not to the
dash).



To get what you want, you need to specify an indent of four for the sequence, and
an offset of two for the dash withing those for spaces. You can do so by inserting,
after the yaml = YAML() line, a line with:



yaml.indent(sequence=4, offset=2)




You can also use your YAML instance itself in a with statement:



import sys
from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

with YAML(output=opath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code)


That will also give you exactly you want:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


You tagged your question with PyYAML as well. Please note that with that package
you cannot do the above: your comments in the YAML file will be lost, and
you don't have the kind of fine control over indentation to get the result you
wanted.






share|improve this answer


























  • Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

    – MEHUL SOLANKI
    Nov 17 '18 at 19:01













  • I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

    – MEHUL SOLANKI
    Nov 17 '18 at 19:30











  • Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

    – Anthon
    Nov 17 '18 at 19:59














0












0








0







The following assumes you are using Python3 (and you should if you are
learning Python), because it uses pathlib that is in the standard
library for Python3. There is a
pathlib2 package that provides that
functionality for Python2.



The example code that you present does a few strange things:




  • it is not necessary to read in the content of a file
    (open(...).read()) and then hand that to the .load() method, you
    can directly pass in the file pointer (i.e. the result from just
    doing open(), or you can use the Path object and hand this to
    .load() without even opening the file (as shown below).


  • it is unclear why a newline is written to sys.stdout, that really
    has no function in this snippet


  • the call code.insert(1, 'sensor', 'None', comment="new key") does put 'sensor' as
    a new key, in position 1 of the dictionary like object code, with the value None
    and the end-of-line comment "new key". (Position counting starts at 0.



If you had really ran that code you would get as output:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
sensor: None # new key
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


At the root of your input file is a mapping with keys Developer,
Support and Martin. The values associate with a key can again be a
mapping (as is the case for all your root-level keys), but they can
also be a sequence (as is the value for skills, indicated by dash
(-) or scalar values (string, number, etc). The root of a YAML
document can be a scalar (which is then the only object in the
document), or a sequence. And a sequence has elements that can be
scalars (as your final line python), but those elements can also be
mappings or other sequences.



Mappings are loaded as Python dicts, sequences are loaded as Python
lists. In order to preserve comments (and other information),
ruamel.yaml will create dict- resp. list-like objects when used as
in the example. You would not be able to call code.insert() on a regular
Python dict but you can do so on what you got back (from YAML().load())



If you just want to add a key-value pair at the end of a dict, you don't need to
use .insert, you can just do:



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


If you need to move something to the end of the YAML file, you actually cannot
just update the value using e.g.



code['Support'] = dict(Department='General', Manager='KLM', Floor='1st', Lab=1)


this will get you the key/value in the old position. You would have to do



del code['Support']


first to make it forget this old position.





As indicated ruamel.yaml supports multiple ways on how to read and
write to a file. The with statement as Moralous answer introduced
is already better than explicit opening, reading and closing. You can also use
pathlib.Path like objects with the .load() and .dump() methods:



from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

yaml = YAML()
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code, opath)


which gives:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


This is almost what you want, but the default indentation of the YAML output
is 2 positions for both the dict (as you want it to be) and for the
sequence (it is counted to the beginning of your element, not to the
dash).



To get what you want, you need to specify an indent of four for the sequence, and
an offset of two for the dash withing those for spaces. You can do so by inserting,
after the yaml = YAML() line, a line with:



yaml.indent(sequence=4, offset=2)




You can also use your YAML instance itself in a with statement:



import sys
from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

with YAML(output=opath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code)


That will also give you exactly you want:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


You tagged your question with PyYAML as well. Please note that with that package
you cannot do the above: your comments in the YAML file will be lost, and
you don't have the kind of fine control over indentation to get the result you
wanted.






share|improve this answer















The following assumes you are using Python3 (and you should if you are
learning Python), because it uses pathlib that is in the standard
library for Python3. There is a
pathlib2 package that provides that
functionality for Python2.



The example code that you present does a few strange things:




  • it is not necessary to read in the content of a file
    (open(...).read()) and then hand that to the .load() method, you
    can directly pass in the file pointer (i.e. the result from just
    doing open(), or you can use the Path object and hand this to
    .load() without even opening the file (as shown below).


  • it is unclear why a newline is written to sys.stdout, that really
    has no function in this snippet


  • the call code.insert(1, 'sensor', 'None', comment="new key") does put 'sensor' as
    a new key, in position 1 of the dictionary like object code, with the value None
    and the end-of-line comment "new key". (Position counting starts at 0.



If you had really ran that code you would get as output:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
sensor: None # new key
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python


At the root of your input file is a mapping with keys Developer,
Support and Martin. The values associate with a key can again be a
mapping (as is the case for all your root-level keys), but they can
also be a sequence (as is the value for skills, indicated by dash
(-) or scalar values (string, number, etc). The root of a YAML
document can be a scalar (which is then the only object in the
document), or a sequence. And a sequence has elements that can be
scalars (as your final line python), but those elements can also be
mappings or other sequences.



Mappings are loaded as Python dicts, sequences are loaded as Python
lists. In order to preserve comments (and other information),
ruamel.yaml will create dict- resp. list-like objects when used as
in the example. You would not be able to call code.insert() on a regular
Python dict but you can do so on what you got back (from YAML().load())



If you just want to add a key-value pair at the end of a dict, you don't need to
use .insert, you can just do:



code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])


If you need to move something to the end of the YAML file, you actually cannot
just update the value using e.g.



code['Support'] = dict(Department='General', Manager='KLM', Floor='1st', Lab=1)


this will get you the key/value in the old position. You would have to do



del code['Support']


first to make it forget this old position.





As indicated ruamel.yaml supports multiple ways on how to read and
write to a file. The with statement as Moralous answer introduced
is already better than explicit opening, reading and closing. You can also use
pathlib.Path like objects with the .load() and .dump() methods:



from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

yaml = YAML()
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code, opath)


which gives:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


This is almost what you want, but the default indentation of the YAML output
is 2 positions for both the dict (as you want it to be) and for the
sequence (it is counted to the beginning of your element, not to the
dash).



To get what you want, you need to specify an indent of four for the sequence, and
an offset of two for the dash withing those for spaces. You can do so by inserting,
after the yaml = YAML() line, a line with:



yaml.indent(sequence=4, offset=2)




You can also use your YAML instance itself in a with statement:



import sys
from pathlib import Path
from ruamel.yaml import YAML

path = Path('inp.yaml') # I shortened this a bit, as I don't have a C: drive
opath = Path('outp.yaml')

with YAML(output=opath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(path)
code['Tabitha'] = dict(name='Tabitha Bitumen', job='Developer', skills=['Java'])
yaml.dump(code)


That will also give you exactly you want:



# Employee records
Developer:
Department: IT
Manager: ABCD
Floor: 2nd
Lab: 4
Support:
Department: General
Manager: XYZ
Floor: 1st
Lab: 1
Martin:
name: Martin Devloper
job: Developer
skills:
- python
Tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- Java


You tagged your question with PyYAML as well. Please note that with that package
you cannot do the above: your comments in the YAML file will be lost, and
you don't have the kind of fine control over indentation to get the result you
wanted.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 15:41

























answered Nov 17 '18 at 15:32









AnthonAnthon

29.7k1693146




29.7k1693146













  • Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

    – MEHUL SOLANKI
    Nov 17 '18 at 19:01













  • I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

    – MEHUL SOLANKI
    Nov 17 '18 at 19:30











  • Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

    – Anthon
    Nov 17 '18 at 19:59



















  • Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

    – MEHUL SOLANKI
    Nov 17 '18 at 19:01













  • I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

    – MEHUL SOLANKI
    Nov 17 '18 at 19:30











  • Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

    – Anthon
    Nov 17 '18 at 19:59

















Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

– MEHUL SOLANKI
Nov 17 '18 at 19:01







Hey sorry for the wrong code, actually I have multiples yaml files in my PC so I was testing with another yaml file which has electronic components name that is why you saw name name "sensor". before posting I didn't noticed it. My bad!

– MEHUL SOLANKI
Nov 17 '18 at 19:01















I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

– MEHUL SOLANKI
Nov 17 '18 at 19:30





I tried running your code but its giving me error "Message=TypeError("'NoneType' object does not support item assignment",) Source=c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py StackTrace: File "c:usersmaster 1tbsourcerepostest_projpythonapplication3pythonapplication3.py", line 12, in <module> yaml.dump(code)". You can also view the screenshot link: (drive.google.com/file/d/1BpXUytPF61My3R0FgyB3MUgKIMI6MVSS/…)

– MEHUL SOLANKI
Nov 17 '18 at 19:30













Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

– Anthon
Nov 17 '18 at 19:59





Check your YAML file ( inp.yaml ) it is probably empty. If you load an empty file then code will be None.

– Anthon
Nov 17 '18 at 19:59













0














The YAML library you're using converts between YAML formatting and Python dictionaries. Therefore you could simple edit the dictionary to add or change a value in the YAML file. For example:



import sys
from ruamel.yaml import YAML
filename = "C:/<Users>/Master/1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml"

yaml = YAML()
with open(filename) as inp_fo:
code = yaml.load(inp_fo)

code["Tabitha"] = {
"name":"Tabitha Bitman",
"job":"Developer",
"skills":["Python"]
}

with open(filename, "w") as file:
yaml.dump(code, file) ##Write to file with new parameter



There is no need to close the files as they were opened in "with"
statements




This will update Tabitha's details in the YAML file, assuming that the rest of your code is correct.



You can also parse a variable into the key:value lookup, replacing code["Tabitha"] with code[variable_name].



If you are working with YAML (or JSON) files I also highly recommend looking into and understanding dictionaries in Python.






share|improve this answer


























  • Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

    – MEHUL SOLANKI
    Nov 17 '18 at 15:34













  • @MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

    – Moralous
    Nov 17 '18 at 16:25






  • 1





    @moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

    – Anthon
    Nov 17 '18 at 17:27
















0














The YAML library you're using converts between YAML formatting and Python dictionaries. Therefore you could simple edit the dictionary to add or change a value in the YAML file. For example:



import sys
from ruamel.yaml import YAML
filename = "C:/<Users>/Master/1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml"

yaml = YAML()
with open(filename) as inp_fo:
code = yaml.load(inp_fo)

code["Tabitha"] = {
"name":"Tabitha Bitman",
"job":"Developer",
"skills":["Python"]
}

with open(filename, "w") as file:
yaml.dump(code, file) ##Write to file with new parameter



There is no need to close the files as they were opened in "with"
statements




This will update Tabitha's details in the YAML file, assuming that the rest of your code is correct.



You can also parse a variable into the key:value lookup, replacing code["Tabitha"] with code[variable_name].



If you are working with YAML (or JSON) files I also highly recommend looking into and understanding dictionaries in Python.






share|improve this answer


























  • Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

    – MEHUL SOLANKI
    Nov 17 '18 at 15:34













  • @MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

    – Moralous
    Nov 17 '18 at 16:25






  • 1





    @moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

    – Anthon
    Nov 17 '18 at 17:27














0












0








0







The YAML library you're using converts between YAML formatting and Python dictionaries. Therefore you could simple edit the dictionary to add or change a value in the YAML file. For example:



import sys
from ruamel.yaml import YAML
filename = "C:/<Users>/Master/1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml"

yaml = YAML()
with open(filename) as inp_fo:
code = yaml.load(inp_fo)

code["Tabitha"] = {
"name":"Tabitha Bitman",
"job":"Developer",
"skills":["Python"]
}

with open(filename, "w") as file:
yaml.dump(code, file) ##Write to file with new parameter



There is no need to close the files as they were opened in "with"
statements




This will update Tabitha's details in the YAML file, assuming that the rest of your code is correct.



You can also parse a variable into the key:value lookup, replacing code["Tabitha"] with code[variable_name].



If you are working with YAML (or JSON) files I also highly recommend looking into and understanding dictionaries in Python.






share|improve this answer















The YAML library you're using converts between YAML formatting and Python dictionaries. Therefore you could simple edit the dictionary to add or change a value in the YAML file. For example:



import sys
from ruamel.yaml import YAML
filename = "C:/<Users>/Master/1TB/source/repos/New_insertdata/PythonApplication3/inp.yaml"

yaml = YAML()
with open(filename) as inp_fo:
code = yaml.load(inp_fo)

code["Tabitha"] = {
"name":"Tabitha Bitman",
"job":"Developer",
"skills":["Python"]
}

with open(filename, "w") as file:
yaml.dump(code, file) ##Write to file with new parameter



There is no need to close the files as they were opened in "with"
statements




This will update Tabitha's details in the YAML file, assuming that the rest of your code is correct.



You can also parse a variable into the key:value lookup, replacing code["Tabitha"] with code[variable_name].



If you are working with YAML (or JSON) files I also highly recommend looking into and understanding dictionaries in Python.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 17:31

























answered Nov 17 '18 at 14:15









MoralousMoralous

71111




71111













  • Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

    – MEHUL SOLANKI
    Nov 17 '18 at 15:34













  • @MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

    – Moralous
    Nov 17 '18 at 16:25






  • 1





    @moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

    – Anthon
    Nov 17 '18 at 17:27



















  • Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

    – MEHUL SOLANKI
    Nov 17 '18 at 15:34













  • @MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

    – Moralous
    Nov 17 '18 at 16:25






  • 1





    @moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

    – Anthon
    Nov 17 '18 at 17:27

















Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

– MEHUL SOLANKI
Nov 17 '18 at 15:34







Hey, Thanks for he answer but my Key value will be changing every time and the yaml file will be edited every time when my key value changes. So can I insert a variable for the key value? Fore example( job{key}: Developer{value}), over here my value Developer will be changing depending upon the employee so It can be "support" as well instead of "Developer". The reason for using a variable is that I will not need to change the code again and again depending on the value.

– MEHUL SOLANKI
Nov 17 '18 at 15:34















@MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

– Moralous
Nov 17 '18 at 16:25





@MEHULSOLANKI Yes you can! Simply replace code["Tabitha"] with code[variable], although the variable must be a string.

– Moralous
Nov 17 '18 at 16:25




1




1





@moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

– Anthon
Nov 17 '18 at 17:27





@moralous There is no need for that to be a string. Python restricts keys to any immutable type, not just strings. E.g. ruamel.yaml can dump dicts that have tuples as key (and it loads them as well).

– Anthon
Nov 17 '18 at 17:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351840%2fhow-to-edit-a-yaml-file-in-python-repeatedly-using-a-variable-without-defining-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini