首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在python中用逗号替换倒数第二个空格

在python中用逗号替换倒数第二个空格
EN

Stack Overflow用户
提问于 2018-06-15 04:23:02
回答 2查看 122关注 0票数 0

我在下面有这个字符串。如何在不使用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#。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-15 04:53:59

您可以使用rfindrsplit

代码语言:javascript
复制
# 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:]
票数 2
EN

Stack Overflow用户

发布于 2018-06-15 05:20:31

Python正则表达式替换?

代码语言:javascript
复制
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'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50865362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档