文章詳情頁
python - 刪除txt文件內所有<>里面的內容包括<>本身。
瀏覽:205日期:2022-08-23 15:34:32
問題描述
刪除txt文件內所有<>里面的內容包括<>本身。該怎么寫?卡在對字符串的處理那里。
問題解答
回答1:大概是這樣:
使用 re.sub 把 <...> 取代為空白:
# python3 codeimport re# for examples = ’123<><here>#$%@#$%^<123>::<a >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
相關文章:
1. node.js - mongodb查找子對象的名稱為某個值的對象的方法2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. html5 - datatables 加載不出來數(shù)據(jù)。4. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題5. docker鏡像push報錯6. phpstudy8.1沒集成mysql-front7. javascript - 在 model里定義的 引用表模型時,model為undefined。8. javascript - QQ第三方登錄的問題9. 測試自動化html元素選擇器元素ID或DataAttribute [關閉]10. 利用IPMI遠程安裝centos報錯!
排行榜

熱門標簽
網公網安備