python reg ex 包含缺少的逗号 [英] python reg ex to include missing commas
问题描述
我需要确保字符串具有逗号分隔值.我读取的字符串可能有空格分隔值.
I need to ensure an string to have comma separated values. The strings I read may have space separated values.
- 我的输入字符串中可能缺少一些逗号,也就是说,如果存在没有逗号的任何空格分隔值,我必须包含一个逗号
- 我不应该对单引号或双引号内的字符串进行任何更改.这些带引号的值可以包含除引号之外的任何字母数字.
一个示例字符串是:
""" 1, ' unchanged 1' " unchanged 2 " 2.009, -2e15 """
我应该在not this1"之后和not this 2"之后加上逗号.
I should include commas after 'not this1' and after " not this 2 ".
所以我的结果字符串应该是:
So my resultant string should be:
"""1,' unchanged 1'," unchanged 2 ",2.009,-2e15"""
我一直在尝试 s1|s2|(s3)
类型的正则表达式.却无法完成任务.
I have been trying s1|s2|(s3)
type regular expressions. But was unable to accomplish the task.
字符串可以有不同数量的用逗号分隔的值.
Strings can have different number of values to be comma separated.
推荐答案
也许使用 findall、str.join 和 str.strip 会更容易,先找到引号之间的字符串,然后找到所有非空格:
Maybe it would be easier use findall, str.join and str.strip, finding the strings between quotes first then all non-whitespace:
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
如果您不希望逗号后有任何空格:
If you don't want any space after the comma:
print(",".join([x.strip(",") for x in r.findall(s)]))
1,' unchanged 1'," unchanged 2 ",2.009,-2e15,3
这篇关于python reg ex 包含缺少的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!