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

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()的第一个参数是分隔符,如果什么都不填就是默认是以空格来分隔