去除網址
我想從換行符 (%OA) 中洗掉 url。在“普通”字串上,您可以使用 .rstrip() 或 strip() 方法,但對于 url,由于某種原因它不起作用。
string = "Hi, Steve\n"
print(string.rstrip()) # Hi, Steve
url = "https://python.iamroot.eu/genindex.html
"
print(url) # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip()) # "https://python.iamroot.eu/genindex.html
"
# I want to get: "https://python.iamroot.eu/genindex.html"
你知道如何解決嗎?
uj5u.com熱心網友回復:
也許這就是你要找的:
url.rstrip('
')
uj5u.com熱心網友回復:
您可以在其中放置要洗掉的字符。
str.rstrip([字符])
回傳洗掉尾隨字符的字串副本。chars 引數是一個字串,指定要洗掉的字符集。如果省略或 None,則 chars 引數默認為洗掉空格。chars 引數不是后綴;相反,它的值的所有組合都被剝離了。
參見 rstrip:https ://docs.python.org/3/library/stdtypes.html#str.rstrip
string = "Hi, Steve\n"
print(string.rstrip()) # Hi, Steve
url = "https://python.iamroot.eu/genindex.html
"
print(url) # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip()) # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip('
'))
# I want to get: "https://python.iamroot.eu/genindex.html"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396786.html
