Algorithm
[python] 특정 문자열이 발견되면 해당 문단을 지워주는 코드
returnzero
2022. 1. 17. 15:29
'''
22/01/17 Choi
Deletes 5 consecutive lines of text that starts with specific string.
'''
import os
#acquire lowest directroy name
#ex) home/sample -> sample
fullpath=os.getcwd()
ls=fullpath.split(os.path.sep)
w1pbtxt=ls[-1]+"_w1.pbtxt"
w2pbtxt=ls[-1]+"_w2.pbtxt"
print(w1pbtxt)
check=0 # used as a sign of indication when deleting 5 consecutive lines
iter=3 #first three text starting with "library { " has to be deleted
with open(w1pbtxt, "r") as f1:
lines = f1.readlines()
with open(w1pbtxt, "w") as f1:
for line in lines:
if iter==0:
f1.write(line)
elif line.strip("\n") != "library {":
if check==0:
f1.write(line)
elif check<4:
check+=1
else:
check=0
iter-=1
print("Iter decreased!\n")
else: check+=1
check=0
iter=3
with open(w2pbtxt, "r") as f2:
lines = f2.readlines()
with open(w2pbtxt, "w") as f2:
for line in lines:
if iter==0:
f2.write(line)
elif line.strip("\n") != "library {":
if check==0:
f2.write(line)
elif check<4:
check+=1
else:
check=0
iter-=1
else: check+=1
OS lab의 데이터 전처리를 위해서 급하게 작성했다.