使用以下正则表达式的下列模式用以提取文本中包含的数字:
pattern = r'[^+\-\d]*([+-]?\d+([.,]\d+)?).*' 模式 1:正整数 text = '123`result = re.match(pattern, text)print(result[1]) # => 123text = '+123`result = re.match(pattern, text)print(result[1]) # => +123 模式 2:负整数 text = '-123`result = re.match(pattern, text)print(result[1]) # => -123 模式 3:小数 text = '123.456`result = re.match(pattern, text)print(result[1]) # => 123.456text = '+123.456`result = re.match(pattern, text)print(result[1]) # => +123.456text = '-123.456`result = re.match(pattern, text)print(result[1]) # => -123.456 模式 4:包含数字和非数字字符 text = '今天气温-10.5度`result = re.match(pattern, text)print(result[1]) # => -10.5 模式 5:当小数点为 ,(欧洲的表示法) text = '今天的气温是-10,5度`result = re.match(pattern, text)print(result[1]) # => -10,5【Python】从文本中提取数字,vaio tz
0evadmin
编程语言
15
文件名:【Python】从文本中提取数字,vaio tz
【Python】从文本中提取数字
同类推荐
-

【PyTorch】数据集,小米2手机
查看 -

【Python CheckiO 题解】Army Battles,小影霸gt430
查看 -

【Python 必会技巧】使用 join() 方法将序列中的元素拼接成字符串,长虹z1怎么样
查看 -

【Python】,acer5542g(pythonh5)
查看 -

【Python】SqlmapAPI调用实现自动化SQL注入安全检测,htc p860
查看 -

【Python】base64模块对图片进行base64编码和解码,猪肉夜里泛出蓝光
查看 -

【Python】json 格式转换 ( json 模块使用 - 列表转 json - json 转列表 - 字典转 json - json 转字典 ),夏新大v进步版
查看 -

【Python】从入门到上头—Python基础(2),pcpop网(《python从入门到精通》)
查看