也许使用findall,str.join和str.strip会更容易,先查找引号之间的字符串,然后查找所有非空格:
s = """ 1, ' unchanged 1' " unchanged 2 " 2.009, -2e15 3"""r = re.compile("['"].*?['"]|S+")print(", ".join([x.strip(",") for x in r.findall(s)]))1, ' unchanged 1', " unchanged 2 ", 2.009, -2e11, ' unchanged 1', " unchanged 2 ", 2.009, -2e15, 35, 3
如果您不想在逗号后留空格:
print(",".join([x.strip(",") for x in r.findall(s)]))1,' unchanged 1'," unchanged 2 ",2.009,-2e15,3
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)