我在下面有字符串。如何在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#是首选。
答案 0 :(得分:2)
您可以使用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:]
答案 1 :(得分:1)
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'