with open('test2.txt','r')as fin:
line = fin.read()
str1 = line.split('\n')
a =0
while a < len(str1):
if "hg38" in str1[a] :
str1.insert(a-1,'\n')
else:
a += 1
print(str1)
test2的内容如下图:
我想要在有hg38的行前面增加一个空行,但是我程序运行不出来,请问有人能够指点一二吗?
你插入后整个数组会变长,然后插入后当前行就变成了‘hg38’的前一行,下次又会进行一次插入,陷入死循环
with open('test2.txt','r') as fin:
line = fin.read()
str1=line.split('\n')
a=0
while a<len(str1):
if 'hg38' in str1[a]:
str1.insert(a,'\n')
a+=1
a+=1
print(str1)
不能直接replace吗,说着re.sub