从字符串中删除非数字字符 [英] Removing non numeric characters from a string

查看:77
本文介绍了从字符串中删除非数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是从文本文件或字符串中删除包括空格在内的所有非数字字符,然后在旧字符旁边打印新结果,例如:

I have been given the task to remove all non numeric characters including spaces from a either text file or string and then print the new result next to the old characters for example:

之前:

sd67637 8

之后:

676378

由于我是初学者,我不知道从哪里开始这项任务.请帮忙

As i am a beginner i do not know where to start with this task. Please Help

推荐答案

最简单的方法是使用正则表达式

The easiest way is with a regexp

import re
a = 'lkdfhisoe78347834 (())&/&745  '
result = re.sub('[^0-9]','', a)

print result
>>> '78347834745'

这篇关于从字符串中删除非数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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