去除python字符串中間的轉義符號


strip()可以去除頭尾的轉義符號,但不能去除字符串中間的

比如title = "13.\t13"

拿到手的就是變量title,無法直接加r變成原始字符串

這時候可以使用repr()方法

print(repr(title))

結果為'13.\t13',發現多了單引號

 

new_title = repr(title).replace(f'\\t', '')
print(new_title)

結果為:
'13.13'

 

再使用eval方法可以轉回字符串

new_title = eval(repr(title).replace(f'\\t', ''))
print(new_title)

結果為:
13.13

 

 參考:

https://blog.csdn.net/qq_33692331/article/details/111414982


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM