I am unable to write a dictionary to file using json.dump
due to having Hebrew letters and the file just contains trash in English letters.
import codecs
if __name__ == '__main__':
k = dict()
k['tony ta'] = 4
k['tt '] = 5
l = list(k)
with codecs.open('text.txt', 'w', 'utf-8') as f:
for i in range (0, len(l)):
f.write('\n'+(l[i]) + ' ' + str(k[l[i]]))
Using codecs I am able to write it correctly and obtain the result I want. For this demo I'm using open
. Is there a more convenient way to do this ?
text.txt
tony ta 4
tt 5
Any better way rather than having to turn my dictionary to list and then access the dictionary to find the value ?