作者opiumeater (opiumeater)
看板Python
标题[问题]想把文章的注脚移到正文後面, 关於re跟zip函数的问题
时间Wed Nov 24 22:04:04 2010
大家好,我最近需要把文章的注脚移动到正文里面,
例如:python*是我的好朋友======点击*号会连结到文章最後的锚点(一种有趣的语言)
改成python(一种有趣的语言),直接写在後面。
因为注脚大概有一千多个,所以想用新学的python试试。
我已经写了一个脚本,可是不知道为何不能用,麻烦各位高手帮我看看哪里错了。
import re
def footnote(path):
myfile = open(path).read()
p1 = re.compile('<a href="#_ftn\d.*?</a>')#*号连结处的正则表达式
p2 = re.compile('<a href="#_ftnref.*?</p>')#文末注释的正则表达式,後面是</p>没错不是笔误
li1 = re.findall(p1,myfile)
li2 = re.findall(p2,myfile)
data = zip(li1,li2)
for i in data:
x = str(i[0])
y = str(i[1])
z = re.sub(x,y,myfile)
open(path,'wb').write(z)
if __name__ == '__main__':
path = raw_input('path:')
footnote(path)
程式在跑时没报错,可是跑完文件却没有更动。
请大家给个方向吧。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.193.212.162
1F:→ sunrise0406:p1跟p2是否真的有找到值? 可以丢个实例麻? 11/24 23:46
2F:→ opiumeater:p1p2都可以找到,p1 = <a href="#_ftn1013" name="_ftn 11/25 00:00
3F:推 dyco:该文件是否被lock主呢? 11/25 16:03