I have a 30000 plus line text file that I need to modify using Python. What I want to do is the following:
-
There is a specific keyword1 in the file that has a certain device name following the keyword1. I need to identify keyword1 in all lines and retrieve all device names, then store them in a list for later use
I have achieved this by using Regular Expression
-
Once I have the device names list, I need to insert a newline with “fixed format + device name” into the file at a series of specific locations following keyword2
This is where I run into problem.
Originally I used the simple counting method. I
pattern.search(line)throughout the file and once the keyword1 is identified, I locate the inserting location by counting i + 5, where i is the index of the line where keyword1 is identified. However, it turns out that the line order in the file does matter so I will need to insert the newline only after the line where keyword2 is located. What’s more troublesome is that keyword2 exists everywhere in the file. It is only those keyword2 within 7 lines after keyword1 that needs to be considered.
To give an example:
This is a random line with KEYWORD2 <--- this keyword2 is not considered
This is a random line
This is a random line
This is a random line, KEYBOARD1 "DEVICE NAME" <--- id keyword1 and record DEVICE
This is a random line
This is a random line
This is a random line
This is a random line
This is a random line with KEYWORD2 <--- this keyword2 is considered
Any suggestion is appreciated. Thanks in advance
I … think you should probably approach it with something like this:
This is using the multiple-file syntax for with that was introduced in 2.7. In earlier versions you’ll have to nest the with statements or manage your file handles manually.