teg*_*ggy 21 python startswith
如何使用startswith函数匹配任何alpha字符[a-zA-Z].例如,我想这样做:
if line.startswith(ALPHA):
Do Something
Run Code Online (Sandbox Code Playgroud)
dan*_*n04 47
如果你想匹配非ASCII字母,你可以这样做:
if line and line[0].isalpha():
Run Code Online (Sandbox Code Playgroud)
efo*_*nis 10
您可以将元组传递给startswiths()(在Python 2.5+中)以匹配其任何元素:
import string
ALPHA = string.ascii_letters
if line.startswith(tuple(ALPHA)):
pass
Run Code Online (Sandbox Code Playgroud)
当然,对于这种简单的情况,正则表达式测试或in运算符将更具可读性.
一个简单的解决方案是使用python regex模块:
import re
if re.match("^[a-zA-Z]+.*", line):
Do Something
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45878 次 |
| 最近记录: |