首页 > 编程语言 > 详细

Python3中字符串中的数字提取方法

时间:2018-10-28 18:29:01      阅读:171      评论:0      收藏:0      [点我收藏+]

Python3中字符串中的数字提取方法

  • re.sub(pattern, repl, string, count=0, flags=0)
1 totalCount = '100abc'
2 totalCount = re.sub("\D", "", totalCount)
  • str.split(sep=None, maxsplit=-1)
>>> import string
>>> a = "32 个答案"
>>> b = a.split()[0]
>>> print(b)
>>> type(b)
<class 'str'>
>>> c = '1,2,3'
>>> c.split(',')
['1', '2', '3']
>>> c.split(',')[0]
'1'
>>> c.split(',')[1]
'2'
>>>

split()的第一个参数是分隔符,如果什么都不填就是默认是以空格来分隔

Python3中字符串中的数字提取方法

原文:https://www.cnblogs.com/TianchiLiu/p/9866188.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!