服了,我怎么知道你什么时候需要 1.2.1 、1.2.2 啊?之前以为是 1.3 、1.4 。所以写的是 1.3 、1.4,你可以参考下,除了 1.2.1 变成了 1.3,1.2.2 变成了 1.4,其他都是一样的:
with open("
test.md", "r+" , encoding='utf-8') as f:
text=f.read()
f.seek(0)
a=0
b=0
c=0
for i in text.splitlines():
print(i)
if 0<i.count('#')<3:
a=a+1
space=i.find(' ')
i=list(i)
i.insert(space+1, str(a)+'. ')
i=''.join(i)
f.write(i+'\n')
f.flush()
b=0
c=a
elif i.count('#')>=3:
b=c+b+0.1
b=round(b,1)
space=i.find(' ')
i=list(i)
i.insert(space+1, str(b)+' ')
i=''.join(i)
f.write(i+'\n')
f.flush()
c=0
else:
f.write(i+'\n')
f.flush()