仅当逗号具有尾随字符时,python才按逗号拆分
这是我的代码:仅当逗号具有尾随字符时,python才按逗号拆分,python,python-3.x,Python,Python 3.x,这是我的代码: with open(dict_file) as record_list: for x in record_list: dictionary_list.append([r for r in x[:-1].split(',')]) 仅当逗号位于字符之间时,我才尝试拆分,例如,“abc,abc”,但如果逗号有尾随空格,则跳过拆分:“12,Main St”使用带lookback和lookahead的正则表达式进行所需的匹配: import re s = "ab
with open(dict_file) as record_list:
for x in record_list:
dictionary_list.append([r for r in x[:-1].split(',')])
仅当逗号位于字符之间时,我才尝试拆分,例如,
“abc,abc”
,但如果逗号有尾随空格,则跳过拆分:“12,Main St”
使用带lookback和lookahead的正则表达式进行所需的匹配:
import re
s = "abc,abc"
print(re.split(r'(?<!\s),(?!\s)', s))
# ['abc', 'abc']
重新导入
s=“abc,abc”
我以前试过,没用,现在成功了:D-谢谢