I just want to write dictionary values into text file line wise line.I can write whole dictionary in to the file using:
log_disk={}
log=open('log.txt','w')
log.write(str(log_disk))
log.close()
Any help will be appreciated.In addition I want to avoid those keys which have value 'Empty' while writing into the file.
First Open the file in write mode by using the File open() method. Then Get the key and value pair using the dictionary items() method from the Dictionary. Iterate over the key values of the dictionary using the 'for' loop and write key and value to a text file by using the write() method.
If you only need the dictionary values -0.3246 , -0.9185 , and -3985 use: your_dict. values() . If you want both keys and values use: your_dict. items() which returns a list of tuples [(key1, value1), (key2, value2), ...] .
Just loop over the values then:
with open('log.txt','w') as log:
for value in log_disk.values():
log.write('{}\n'.format(value))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With