从字符串中删除非数字字符
问题描述:
我的任务是从文本文件或字符串中删除包括空格在内的所有非数字字符,然后在旧字符旁边打印新结果,例如:
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'