删除所有<word>标签</word>

时间:2012-03-08 18:46:22

标签: python regex

我必须编写一个程序来删除<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),它表示我在运行程序时期望一个字符串或缓冲区。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您正在尝试替换为字符串“p”。但是,p是findall的结果,它是一个列表。

我建议这样做:

lines = file_variable.read()
print re.subn('<.*?>', ' ', line)

答案 1 :(得分:0)

lines包含您应传递给subn

的字符串
print substitution.subn( ' ', lines )