【发布时间】:2019-07-18 09:34:03
【问题描述】:
我拆分了从 ( 到 ) 开始的数据 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
【问题讨论】:
-
你有两次
endswith(),但你没有使用startswith() -
请考虑在您的问题中正确格式化代码,并且不要忘记发布错误回溯,正如@furas 所说。
-
split("()")不会在"("和")"上拆分,而只会在"()"上拆分 -
re.split(r'[()]', strin)将在(或)上进行拆分 -
编辑您的数据,使其更具可读性并显示预期结果。
标签: python startswith ends-with