Python字符串中如何去除数字之间的逗号
作者:逗逗家本逗 时间:2023-12-01 22:09:48
字符串去除数字间的逗号
在西文数字的表示中,很多格式是类似这样:123,456,789。
如果得到这样的一个字符串,直接用int转换成整型肯定报错,那么在格式转换前需要先去除数字之间的逗号 。
如果字符串只有数字和“,”,那么可以用一个replace替换。
例如:
>>> n = '123,456,789'
>>> n1 = n.replace(',','')
>>> print n1
123456789
但是,如果当字符串中包括数字和其他字符,replace替换就有点霸道了。例如:Today is Sunday, I bought $ 100,000. 直接用replace替换会把字符串中的逗号标点符号也删除了。
所以需要找到 数字,数字 这种格式之前的逗号。
代码类似如下:
import re
s = 'Today is Sunday, I bought $ 100,000.'
p = re.compile(r'\d,\d')
while 1:
m = p.search(s)
if m:
mm = m.group()
s = s.replace(mm,mm.replace(',',''))
else:
break
print s
用正则表达式判断。
删除字符串中的符号
删除字符串中的指定符号
s = "abc123,123。"
# 删除逗号
s = s.replace(',', '')
print(s) # "abc123123。"
删除字符串中的空格
s = " 123abc "
# 删除开头的空格
print(s.lstrip()) # "123 abc "
# 删除结尾的空格
print(s.rstrip()) # " 123 abc"
# 删除开头和结尾的空格
print(s.strip()) # "123 abc"
# 删除字符串中所有的空格
print(s.replace(' ', '')) # "123abc"
删除字符串中的所有符号,只保留数字和英文字母
import re
s = "123,abc .?/&?》^_^dddA。"
# 把所有编码非\u0030-\u0039(数字)、\u0041-\u007a(英文字母)的字符替换为空字符串
rs = re.sub("([^\u0030-\u0039\u0041-\u007a])", '', s)
print(rs) # "123abcdddA"
只字符串中的保留汉字
import re
s = "我爱中国🇨🇳,I love China。"
# 把所有编码非\u4e00-\u9fa5(汉字)的字符替换为空字符串
rs = re.sub("([^\u4e00-\u9fa5])", '', s)
print(rs) # "我爱中国"
对应的unicode编码范围
说明 | unicode范围 |
---|---|
数字 | \u0030-\u0039 |
汉字 | \u4e00-\u9fa5 |
大写字母 | \u0041-\u005a |
小写字母 | \u0061-\u007a |
英文字母 | \u0041-\u007a |
韩文 | \uAC00-\uD7AF |
日文 | \u3040-\u31FF |
来源:https://blog.csdn.net/weixin_42891455/article/details/113883125
标签:Python,字符串,数字,逗号
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用matplotlib绘制余弦的散点图示例
2023-04-08 03:56:29
![](https://img.aspxhome.com/file/2023/5/109885_0s.png)
浅谈validator自定义验证及易错点
2024-05-02 16:58:41
![](https://img.aspxhome.com/file/2023/5/130105_0s.png)
SQL server中字符串逗号分隔函数分享
2024-01-12 17:20:22
![](https://img.aspxhome.com/file/2023/8/108488_0s.jpg)
使用Postman生成的okhttp代码依赖
2023-06-12 22:34:12
![](https://img.aspxhome.com/file/2023/8/58168_0s.png)
使用graphics.py实现2048小游戏
2021-03-20 01:47:19
![](https://img.aspxhome.com/file/2023/8/115398_0s.jpg)
对numpy Array [: ,] 的取值方法详解
2023-02-05 17:33:23
小结下dom节点操作
2011-03-08 10:33:00
asp之自动闭合HTML/ubb标签函数附简单注释
2011-04-04 11:18:00
pycharm2020.1.2永久破解激活教程,实测有效
2021-11-01 15:17:57
![](https://img.aspxhome.com/file/2023/4/89224_0s.png)
Golang将Map的键值对调的实现示例
2024-04-27 15:32:40
![](https://img.aspxhome.com/file/2023/0/134050_0s.png)
PHP制作图形验证码代码分享
2024-05-11 09:26:01
![](https://img.aspxhome.com/file/2023/7/126207_0s.png)
django实现支付宝支付实例讲解
2023-08-27 04:45:44
![](https://img.aspxhome.com/file/2023/1/62551_0s.png)
asp用正则解析远程图片地址,用XMLHTTP将其保存
2007-10-26 12:34:00
如何列出SQL数据库中的存储过程?
2010-01-12 19:58:00
详解Django中Request对象的相关用法
2021-12-08 16:30:06
![](https://img.aspxhome.com/file/2023/6/99436_0s.jpg)
python机器学习基础K近邻算法详解KNN
2024-01-01 05:44:57
![](https://img.aspxhome.com/file/2023/9/78739_0s.png)
发个选星星打分/投票功能函数
2008-05-22 12:38:00
mybatis统计每条SQL的执行时间的方法示例
2024-01-28 12:54:43
python dict remove数组删除(del,pop)
2022-11-17 05:24:03
Python 数据可视化之Matplotlib详解
2022-12-30 05:33:21
![](https://img.aspxhome.com/file/2023/6/101156_0s.png)