您的位置:首页技术文章
文章详情页

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

浏览:62日期:2022-08-23 15:34:32

问题描述

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

问题解答

回答1:

大概是這樣:

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

# python3 codeimport re# for examples = ’123<><here>#$%@#$%^<123>::<a 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

标签: Python 编程
相关文章: