我有一个类似如下的数据:
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发布于 2020-03-25 19:31:47
你可以试试这段代码:
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发布于 2020-03-25 19:36:04
在下面尝试一下:
s = "Crystal Palace, London"
output = s.split(',')[0].replace(' ', '-').lower()
print(output)https://stackoverflow.com/questions/60847934
复制相似问题