python - 删除txt文件内所有<>里面的内容包括<>本身。
ringa_lee
ringa_lee 2017-04-18 10:27:36
0
1
718

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

ringa_lee
ringa_lee

ringa_lee

Antworte allen(1)
大家讲道理

大概是這樣:

使用 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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!