Pythonic Way Of Inserting Lines To A File
So as the description describes, I would like to add text to a file in a sequential manner. Say, for example I have a file like this (NOT HTML, this is just an imaginary language)
Solution 1:
I would do it like this:
with open(outputfile,'w') asout, open(inputfile) as f:
for line in f:
out.write(line)
if tag_match(line): #Somehow determine ifthis line is a match where we want to insert text.
out.write('\n'.join(fruits)+'\n')
You might come up with a way to make it faster, but I doubt it is worthwhile. This is simple, easy to read, and gets the job done. "pythonic" enough for me :-)
Post a Comment for "Pythonic Way Of Inserting Lines To A File"