Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing only dictionary values into a text file

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.

like image 329
user3787457 Avatar asked Jul 14 '14 12:07

user3787457


People also ask

How do you write a dictionary to a text file in Python?

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.

How do you only get values from a dictionary?

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), ...] .


1 Answers

Just loop over the values then:

with open('log.txt','w') as log:
    for value in log_disk.values():
        log.write('{}\n'.format(value))
like image 134
Martijn Pieters Avatar answered Sep 21 '22 22:09

Martijn Pieters