在Python中,如何检查字符串是否只包含某些字符?
我需要检查一个只包含a..z,0..9和的字符串.(期间),没有其他性格.
我可以迭代每个字符并检查字符是a ..z或0..9,或.但那会很慢.
我现在还不清楚如何使用正则表达式来完成它.
它是否正确?你能建议一个更简单的正则表达式或更有效的方法吗?
#Valid chars . a-z 0-9
def check(test_str):
import re
#http://docs.python.org/library/re.html
#re.search returns None if no position in the string matches the pattern
#pattern to search for any character other then . a-z 0-9
pattern = r'[^\.a-z0-9]'
if re.search(pattern, test_str):
#Character other then . a-z 0-9 was found
print 'Invalid : %r' % (test_str,)
else:
#No character other then . a-z 0-9 was found
print 'Valid : %r' % (test_str,)
check(test_str='abcde.1')
check(test_str='abcde.1#')
check(test_str='ABCDE.12')
check(test_str='_-/>"!@#12345abcde<')
''' …Run Code Online (Sandbox Code Playgroud) 我被赋予了从文本文件或字符串中删除所有非数字字符(包括空格)的任务,然后在旧字符旁边打印新结果,例如:
之前:
sd67637 8
Run Code Online (Sandbox Code Playgroud)
后:
sd67637 8 = 676378
Run Code Online (Sandbox Code Playgroud)
由于我是初学者,我不知道从哪里开始这项任务.请帮忙
def get_digits(str1):
c = ""
for i in str1:
if i.isdigit():
c += i
return c
Run Code Online (Sandbox Code Playgroud)
上面是我使用的代码,问题是它只返回字符串的第一个数字.为此,我必须保持for循环和return语句.谁知道如何解决?
谢谢.
我需要从字符串中替换非数字字符.
例如,"8-4545-225-144"需要为"84545225144"; "$ 334fdf890 == - "必须是"334890".
我怎样才能做到这一点?
我有一个庞大的文本语料库(逐行),我想删除特殊字符,但维持字符串的空间和结构.
hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.
Run Code Online (Sandbox Code Playgroud)
应该
hello there A Z R T world welcome to python
this should be the next line followed by another million like this
Run Code Online (Sandbox Code Playgroud) 我有像'12454v','346346z'这样的字符串.我想删除字符串中的所有字母.
重新工作正常:
import re
str='12454v'
re.sub('[^0-9]','', str)
#return '12454'
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用正则表达式的情况下执行此操作?
我刚刚从Python格式中学习了Python中的货币, Python模块babel提供了babel.numbers.format_currency将数字格式化为货币.例如,
from babel.numbers import format_currency
s = format_currency(123456.789, 'USD', locale='en_US') # u'$123,456.79'
s = format_currency(123456.789, 'EUR', locale='fr_FR') # u'123\xa0456,79\xa0\u20ac'
Run Code Online (Sandbox Code Playgroud)
反过来,从货币到数字,如$123,456,789.00- > 123456789?babel提供babel.numbers.parse_number解析本地数字,但我没有找到类似的东西parse_currency.那么,将本地货币解析为数字的理想方法是什么?
# Way 1
import string
all=string.maketrans('','')
nodigs=all.translate(all, string.digits)
s = '$123,456.79'
n = s.translate(all, nodigs) # 12345679, lost `.`
# Way 2
import re
n = re.sub("\D", "", s) # 12345679
Run Code Online (Sandbox Code Playgroud)
它不关心小数分隔符..
.从字符串中删除除(,参见 …
我想知道如何按内部数字对字符串进行排序.
例如,我有:
hello = " hola %d" % (number_from_database)
bye = "adios %d" % (number_from_database_again)
Run Code Online (Sandbox Code Playgroud)
我想用数字对它们进行排序,即使它发生了变化.
我调用一个函数返回各种字符的代码,范围从(到",和,和数字).
是否有一种优雅的方式来删除所有这些,所以我最终只有字母?
我的问题很简单,我试图从字符串中删除任何不是 AZ 或 0-9 的字符。
基本上这是我想要做的过程:
whitelist=['a',...'z', '0',...'9']
name = '_abcd!?123'
name.strip(whitelist)
print(name)
>>> abcd123
Run Code Online (Sandbox Code Playgroud)
重要的是要知道我不能只打印名称中的有效字符。我需要实际使用处于更改状态的变量。
我有以下字符串:
text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
Run Code Online (Sandbox Code Playgroud)
我想回来:
2007 2008
Run Code Online (Sandbox Code Playgroud)
有什么办法在Python中做到这一点?
这有效:
stripped_str = whatever_str.rstrip("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Run Code Online (Sandbox Code Playgroud)
但对我来说似乎非常不优雅.这样做的更干净的方式?