How can I write the following using Python with -c
to execute a one-liner loop?
import json
dictionary = {"one":"two", "three":"four" }
with open("data.txt", "r+") as f:
data = f.read()
for key in dictionary.keys():
data = data.replace(key, dictionary[key])
f.seek(0)
f.write(data)
f.truncate()
I tried:
python3 -c $'import json;dictionary = dictionary = {"one":"two", "three":"four" }\nwith open("data.txt", "r+") as f: f.seek(0); data = f.read()\nfor key in dictionary.keys(): data = data.replace(key, dictionary[key]); f.write(data)'
and got:
Traceback (most recent call last):
File "<string>", line 3, in <module>
ValueError: I/O operation on closed file.