Python 从字符串中删除非数字字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17336943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing non numeric characters from a string
提问by Obcure
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:
我的任务是从文本文件或字符串中删除所有非数字字符,包括空格,然后在旧字符旁边打印新结果,例如:
Before:
前:
sd67637 8
After:
后:
sd67637 8 = 676378
As i am a beginner i do not know where to start with this task. Please Help
由于我是初学者,我不知道从哪里开始这项任务。请帮忙
采纳答案by mar mar
The easiest way is with a regexp
最简单的方法是使用正则表达式
import re
a = 'lkdfhisoe78347834 (())&/&745 '
result = re.sub('[^0-9]','', a)
print result
>>> '78347834745'
回答by Jon Clements
Loop over your string, char by char and only include digits:
循环遍历您的字符串,一个字符一个字符并且只包含数字:
new_string = ''.join(ch for ch in your_string if ch.isdigit())
Or use a regex on your string (if at some point you wanted to treat non-contiguous groups separately)...
或者在您的字符串上使用正则表达式(如果在某个时候您想单独处理非连续组)...
import re
s = 'sd67637 8'
new_string = ''.join(re.findall(r'\d+', s))
# 676378
Then just print
them out:
然后print
把它们拿出来:
print(old_string, '=', new_string)
回答by Saullo G. P. Castro
You can use string.ascii_letters
to identify your non-digits:
您可以使用string.ascii_letters
来识别您的非数字:
from string import *
a = 'sd67637 8'
a = a.replace(' ', '')
for i in ascii_letters:
a = a.replace(i, '')
In case you want to replace a colon, use quotes "
instead of colons '
.
如果您想替换冒号,请使用引号"
而不是冒号'
。
回答by Inbar Rose
There is a builtinfor this.
有一个内置的。
string.translate(s, table[, deletechars])
Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
string.translate(s, table[, deletechars])
从 s 中删除 deletechars 中的所有字符(如果存在),然后使用 table 转换字符,它必须是一个 256 字符的字符串,给出每个字符值的转换,按其序数索引。如果 table 为 None,则仅执行字符删除步骤。
>>> import string
>>> non_numeric_chars = ''.join(set(string.printable) - set(string.digits))
>>> non_numeric_chars = string.printable[10:] # more effective method. (choose one)
'sd67637 8'.translate(None, non_numeric_chars)
'676378'
Or you could do it with no imports (but there is no reason for this):
或者你可以在没有进口的情况下做到这一点(但没有理由这样做):
>>> chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~ \t\n\r\x0b\x0c'
>>> 'sd67637 8'.translate(None, chars)
'676378'