I am looping through a file and I need to insert a text block above a line when it matches a particular string. Please help!
convBlockMoved = False
for line in outfile:
if(line.startswith('mappingSchemeAxis') and not convBlockMoved):
for convLine in conversionBlocks:
print convLine, #Print this above line
convBlockMoved = True
Note: conversionBlocks is a String array
So if your file is not huge, you can read all the lines at once and then work with lists. An example using the insert method of lists would be: