在 Python 中,我尝试使用正则表达式对 CSV 字符串中的数字进行空格填充。我当然可以使用 split()、format() 和 join() 来完成此操作,但我的限制是使用正则表达式。为什么以下 re.sub
仅对找到的匹配项 0、2、4、6、8 进行替换?它会跳过所有其他匹配。
import re
# trying to pad the single-digit numbers in a CSV with a single leading space
# input '1,2,12,14' would produce output ' 1, 2,12,14' (notice leading spaces on single-digit numbers)
s = '0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35'
print(s)
# first, add commas to front and end so that first and final numbers are captured by the regex
s = ',' + s + ','
# the main regex
s = re.sub(r',([0-9]{1}),', r', \1,', s)
# lastly, strip the front and end commas we added before the regex
s = s.strip(',')
print(s)
...上面生成以下内容,并显示输入字符串和输出字符串:
0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35
0,1, 2,3, 4,5, 6,7, 8,9,28,29,30,31,32,33,34,35
所以我只是好奇 re.sub()
在这里表现的技术原因。
最佳答案
您得到该结果是因为模式 ,([0-9]),
匹配逗号、单个数字、逗号。在此字符串中(您还添加了逗号),0,1,2,3,4,5,6,7,8,
它只能匹配,0,
然后 ,2,
等等...
要使用单个前导空格填充个位数,您可以简化脚本,省去使用 strip 和附加逗号,并使用模式的正向前瞻。
请注意,可以省略量词 {1}
。
,([0-9])(?=,)
例如
import re
s = '0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35'
regex = r',([0-9])(?=,)'
s = re.sub(regex, r', \1', s)
print(s)
输出
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,28,29,30,31,32,33,34,35
如果您还想支持仅填充单个数字,则可以更新末尾匹配处的正向前瞻,或者使用逗号或断言字符串的结尾:
,([0-9])(?=,|$)
编辑
由于您还想附加和前置逗号并填充第一个数字,因此您可以将模式更新为 (?:,|^)([0-9])(?=,|$)
import re
s = '0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35'
s = ',' + s + ','
s = re.sub(r'(?:,|^)([0-9])(?=,|$)', r', \1', s)
s = s.strip(',')
print(s)
输出
0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35
关于python - 使用正则表达式在 CSV 字符串中填充空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019629/