替换两个数字之间的空格字符 [英] Replace space character between two numbers

查看:43
本文介绍了替换两个数字之间的空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用逗号替换两个数字之间的空格

15.30 396.90 =>15.30,396.90

在 PHP 中使用:

'/(?<=\d)\s+(?=\d)/', ','

如何用 Python 实现?

解决方案

有几种方法可以做到(抱歉,Python 之禅).使用哪一个取决于您的输入:

<预><代码>>>>s = "15.30 396.90">>>",".join(s.split())'15.30,396.90'>>>s.replace(" ", ",")'15.30,396.90'

或者,使用re,例如,这样:

<预><代码>>>>进口重新>>>re.sub("(\d+)\s+(\d+)", r"\1,\2", s)'15.30,396.90'

I need to replace space with comma between two numbers

15.30 396.90 => 15.30,396.90

In PHP this is used:

'/(?<=\d)\s+(?=\d)/', ','

How to do it in Python?

解决方案

There are several ways to do it (sorry, Zen of Python). Which one to use depends on your input:

>>> s = "15.30 396.90"
>>> ",".join(s.split())
'15.30,396.90'
>>> s.replace(" ", ",")
'15.30,396.90'

or, using re, for example, this way:

>>> import re
>>> re.sub("(\d+)\s+(\d+)", r"\1,\2", s)
'15.30,396.90'

这篇关于替换两个数字之间的空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆