If condition with startswith and endswith 错误
If condition with startswith and endswith error
我拆分了从(和结束)开始的数据
x 包含类似 (33)Knoxville, TN,(1)Basking Ridge, NJ 这样的数据
location = "".join(x.split("()"))[4:] 在这个拆分逻辑中我应该给什么条件 [3:] ??
if name:
if x.startswith('(') and x.endswith(')'):
location = "".join(x.split("()"))[3:]
print(location)
else:
location = x
希望您尝试按 (chars)
或 ,
、
拆分
>>> s = '(1)Basking Ridge, NJ (33)Knoxville, TN'
>>> import re
>>> re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
['', 'Basking Ridge', 'NJ', 'Knoxville', 'TN']
>>> t = re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
>>> ','.join([i for i in t if i])
'Basking Ridge,NJ,Knoxville,TN'
>>>
我拆分了从(和结束)开始的数据 x 包含类似 (33)Knoxville, TN,(1)Basking Ridge, NJ 这样的数据 location = "".join(x.split("()"))[4:] 在这个拆分逻辑中我应该给什么条件 [3:] ??
if name:
if x.startswith('(') and x.endswith(')'):
location = "".join(x.split("()"))[3:]
print(location)
else:
location = x
希望您尝试按 (chars)
或 ,
、
>>> s = '(1)Basking Ridge, NJ (33)Knoxville, TN'
>>> import re
>>> re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
['', 'Basking Ridge', 'NJ', 'Knoxville', 'TN']
>>> t = re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
>>> ','.join([i for i in t if i])
'Basking Ridge,NJ,Knoxville,TN'
>>>