Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Local Search
- Search Algorithm
- AWS
- 코드
- 문자열
- 파이썬
- 알고리즘
- 철학
- typescript
- 배포
- 문자열처리
- 컴퓨터공학
- node배포
- node.js
- CS
- multer-s3
- Hill Climbing
- lightsailor
- 컴퓨터과학
- Simulated Annealing
- computerscience
Archives
- Today
- Total
지식의모듈화
[python] 특정 문자열이 발견되면 해당 문단을 지워주는 코드 본문
'''
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의 데이터 전처리를 위해서 급하게 작성했다.
'Algorithm' 카테고리의 다른 글
[에라토스테네스의 체] 4948번 베르트랑 공준 (0) | 2022.07.01 |
---|---|
[LIS] 최장증가부분수열 알고리즘 (0) | 2022.06.27 |