首页  >  问答  >  正文

python - 删除txt文件内所有<>里面的内容包括<>本身。

删除txt文件内所有<>里面的内容包括<>本身。该怎么写?
卡在对字符串的处理那里。

ringa_leeringa_lee2741 天前726

全部回复(1)我来回复

  • 大家讲道理

    大家讲道理2017-04-18 10:29:36

    大概是这样:

    使用 re.sub<...> 取代为空白:

    # python3 code
    
    import re
    
    # for example
    s = '123<><here>#$%@#$%^<123>::<a class="haha" href="http://www.hello.com">haha'
    print(re.sub('<[^>]*>', '', s))
    
    # read txt and remove all <...>
    with open('your.txt', 'r') as reader:
        for line in reader:
            line = line.strip()
            print(re.sub('<[^>]*>', '', line))

    范例字串取代结果:

    123#$%@#$%^::haha

    我回答过的问题: Python-QA

    回复
    0
  • 取消回复