我在下面有这个字符串。如何在不使用python中的for循环的情况下替换倒数第二个空格。
"814409014860", "BOA ", "604938XXXXXX5410 ",,"ADOM ","ADU SAVBOSS SVGIDIIADOM0001 Int. charge ","24/05/18 09:39:08 0.70 ",0.00
我想避免for循环,因为数据量很大。
逗号紧跟在分钟时间戳之后
09:39:08 0.70 ",0.00
=>09:39:08, 0.70 ",0.00
最好是Python、bash、c#。
发布于 2018-06-15 04:53:59
您可以使用rfind和rsplit
# First find the occurrence of the last space
last_space_index = big_string.rfind(" ")
# then split from the right the substring that ends in that index
new_big_string = ", ".join(big_string[:last_space_index].rsplit(" ", 1)) + big_string[last_space_index:]
发布于 2018-06-15 05:20:31
Python正则表达式替换?
import re
s = '"814409014860", "BOA ", "604938XXXXXX5410 ",,"ADOM ","ADU SAVBOSS SVGIDIIADOM0001 Int. charge ","24/05/18 09:39:08 0.70 ",0.00'
pattern = r'(\d{2}\:\d{2}\:\d{2})'
output = re.sub(pattern,r'\1,',s)
print(output)
>'"814409014860", "BOA ", "604938XXXXXX5410 ",,"ADOM ","ADU SAVBOSS SVGIDIIADOM0001 Int. charge ","24/05/18 09:39:08, 0.70 ",0.00'
https://stackoverflow.com/questions/50865362
复制相似问题