作者sunrise0406 (baker)
看板Python
标题Re: [问题]想把文章的注脚移到正文後面, 关於re跟 …
时间Thu Nov 25 00:39:23 2010
※ 引述《opiumeater (opiumeater)》之铭言:
: 大家好,我最近需要把文章的注脚移动到正文里面,
: 例如:python*是我的好朋友======点击*号会连结到文章最後的锚点(一种有趣的语言)
: 改成python(一种有趣的语言),直接写在後面。
: 因为注脚大概有一千多个,所以想用新学的python试试。
: 我已经写了一个脚本,可是不知道为何不能用,麻烦各位高手帮我看看哪里错了。
: 1 import re
: 2 def footnote(path):
: 3 myfile = open(path).read()
: 4 p1 = re.compile('<a href="#_ftn\d.*?</a>')#*号连结处的正则表达式
: 5 p2 = re.compile('<a href="#_ftnref.*?</p>')#文末注释的正则表达式,後面是</p>没错不是笔误
: 6 li1 = re.findall(p1,myfile)
: 7 li2 = re.findall(p2,myfile)
: 8 data = zip(li1,li2)
: 9 for i in data:
: 10 x = str(i[0])
: 11 y = str(i[1])
: 12 z = re.sub(x,y,myfile)
: 13 open(path,'wb').write(z)
: 14 if __name__ == '__main__':
: 15 path = raw_input('path:')
: 16 footnote(path)
: 程式在跑时没报错,可是跑完文件却没有更动。
: 请大家给个方向吧。
修改9~13
for pattern_repl in data:
myfile = myfile.replace(pattern_repl[0],pattern_repl[1],1)
open(path,'w').write(myfile)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.166.94