我必须编写一个程序来删除<word>
和</word>
形式的所有表达式,其中word是任意字母序列(大写和小写)和
删除表单<word ..... >
和</word>
的所有表达式,其中单词与之前相同。例如,删除<a href=”wwang3.htm” class=”c l”>
到目前为止,我的代码看起来像这样:
def remove_1( file_location ):
""""""
import re
file_variable = open( file_location )
lines = file_variable.read()
p = re.findall('<.*?>', lines)
print p
substitution = re.compile('<.*?>')
print substitution.subn( ' ', p )
我收到一个错误,该错误指向print.substitution.subn( ' ', p)
,它表示我在运行程序时期望一个字符串或缓冲区。非常感谢任何帮助。
答案 0 :(得分:1)
您正在尝试替换为字符串“p”。但是,p是findall的结果,它是一个列表。
我建议这样做:
lines = file_variable.read()
print re.subn('<.*?>', ' ', line)
答案 1 :(得分:0)
lines
包含您应传递给subn
print substitution.subn( ' ', lines )