[英]Return first word of the string
我有如下數據:
Colindale London
London Borough of Bromley
Crystal Palace, London
Bermondsey, London
Camden, London
這是我的代碼:
def clean_whitespace(s):
out = str(s).replace(' ', '')
return out.lower()
我的代碼現在只返回已刪除空格的字符串。 如何選擇字符串的第一個單詞。 例如:
Crystal Palace, London -> crystal-palace
Bermondsey, London -> bermondsey
Camden, London -> camden
你可以試試這個代碼:
s = 'Bermondsey, London'
def clean_whitespace(s):
out = str(s).split(',', 1)[0]
out = out.strip()
out = out.replace(' ', '-')
return out.lower()
print(clean_whitespace(s))
輸出:
bermondsey
試試這個:
s = "Crystal Palace, London"
output = s.split(',')[0].replace(' ', '-').lower()
print(output)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.