我尝试了几次,但是在正则表达式上却失败了。我想将所有双引号替换为单引号,而不是特殊情况。 "[\"sa\"]" become '[\"sa\"]'
输入
"35457680047217","1","425121","2018-07-15 00:00:00.000",,,,"10.0","0",,"0",,"0",,,"0","0","0","0",,"[\"23\"]","[\"20\"]","[\"2\",\"58\"]","[\"0\",\"1\",\"2\",\"3\",\"4\"]","2",,"0",,"2018-07-15 00:00:00.000","2018-07-15 00:00:00.000","22",,,,"3",
输出
'35457680047217','1','425121','2018-07-15 00:00:00.000',,,,'10.0','0',,'0',,'0',,,'0','0','0','0',,'[\"23\"]','[\"20\"]','[\"2\",\"58\"]','[\"0\",\"1\",\"2\",\"3\",\"4\"]','2',,'0',,'2018-07-15 00:00:00.000','2018-07-15 00:00:00.000','22',,,,'3',
答案 0 :(得分:1)
您可以使用以下正则表达式:(^|[^\\])"
。它寻找双引号("
),其后是字符串的开头或不是\
的东西。进行全局匹配,并替换为\1'
。
在regex01上的演示
答案 1 :(得分:0)
您可以基于逗号将字符串拆分为多个单词,但将[.*]
视为一个整体元素,在单词边界处将"
替换为"
,然后再将单词重新连接起来>
>>> s = '"35457680047217","1","425121","2018-07-15 00:00:00.000",,,,"10.0","0",,"0",,"0",,,"0","0","0","0",,"[\"23\"]","[\"20\"]","[\"2\",\"58\"]","[\"0\",\"1\",\"2\",\"3\",\"4\"]","2",,"0",,"2018-07-15 00:00:00.000","2018-07-15 00:00:00.000","22",,,,"3",'
>>> new_s = ','.join(map(lambda w: re.sub('(^")|("$)', "'", w), re.split(r',(?="\[)|(?<!=\]"),', s)))
>>> print(new_s)
'35457680047217','1','425121','2018-07-15 00:00:00.000',,,,'10.0','0',,'0',,'0',,,'0','0','0','0',,'["23"]','["20"]','["2','58"]','["0','1','2','3','4"]','2',,'0',,'2018-07-15 00:00:00.000','2018-07-15 00:00:00.000','22',,,,'3',
>>>