SVill :
I'm creating a program to update a text file, that has a list of cities:
New York City
New York
USA
Newark
New Jersey
USA
Toronto
Ontario
Canada
If I wanted to delete the details for Newark using a bash script, I can do this:
sed -i "/Newark/,+3d" test.txt
And that would leave me with the following:
New York City
New York
USA
Toronto
Ontario
Canada
However, I would like to do this in Python, and I'm having issues figuring out how to delete the following lines, after the line with Newark. I can delete Newark:
with open('test.txt') as oldfile, open('test2.txt', 'w') as newfile:
for line in oldfile:
if not "Newark" in line:
newfile.write(line)
os.remove('test.txt')
os.rename('test2.txt', 'test.txt')
But this does nothing for the remaining two lines, and creates a new file I then have to use to replace the original file.
- How can I go about mimicing the sed command's functionality with Python?
- Is there any way to do an in file edit, so I do not have to create and replace the file everytime I need to delete from it?
Raphael Medaer :
With a counter ? Here it is:
with open('test.txt') as oldfile, open('test2.txt', 'w') as newfile:
skip = 0
for line in oldfile:
if "Newark" in line:
skip = 3
elif skip > 0:
skip = skip - 1
else:
newfile.write(line)
Edit:
I only answered the first question. fileinput support file editing, please have a look here: How to search and replace text in a file? Btw, I would recommend also in-place because it doesn't hijack stdout
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=387058&siteId=1