I have a dict with a few {key, value} pairs. I also have a file with some content.
The file is something like this:
some random text
...
...
text-which-matches-a-key
some important lines
...
...
some other important text until end of file
What I want is, to search/iterate through the file until a line matches a key of the dict, the append the corresponding value before/after some important lines
What I've tried to do is this:
with open('file', 'a+') as f:
for key in a:
if f.readlines() == key:
f.writelines(a[key])
f.close()
where a is a dict, with many key,value pairs.
I'd be happy if the results are something like:
some random text
...
...
text-which-matches-a-key
some important lines
value corresponding to the key
...
...
some other important text until end of file
or:
some random text
...
...
text-which-matches-a-key
value corresponding to the key
some important lines
...
...
some other important text until end of file
Any help is appreciated.
P.S: Using Python 2.7, on PyCharm, on Windows 10.