String start or end match
You need to check the beginning or end of a string through the specified text pattern , For example, file name suffix , URL Scheme wait .
A simple way to check the beginning or end of a string is to use str.startswith() Or is it str.endswith() Method . such as ：
'spam.txt' > filename.endswith('.txt') True > filename.startswith('file:') False > url = 'http://www.python.org' > url.startswith('http:') True >> filename =
If you want to check multiple matches, maybe , Just put all the matches in a tuple , Then pass it to startswith() perhaps endswith() Method ：
'.') > filenames [ 'Makefile', 'foo.c', 'bar.py', 'spam.c', 'spam.h' ] > [name for name in filenames if name.endswith(('.c', '.h')) ] ['foo.c', 'spam.c', 'spam.h' > any(name.endswith('.py') for name in filenames) True >> import os > filenames = os.listdir(
from urllib.request import urlopen def read_data(name): if name.startswith(('http:', 'https:', 'ftp:')): return urlopen(name).read() else: with open(name) as f: return f.read()
Strangely enough , In this method, a tuple must be entered as a parameter . If you happen to have one list perhaps set Type options , Be sure to call... Before passing parameters tuple() Convert it to a tuple type . such as ：
'http:', 'ftp:'] > url = 'http://www.python.org' > url.startswith(choices) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: startswith first arg must be str or a tuple of str, not list > url.startswith(tuple(choices)) True >> choices = [
startswith() and endswith() Method provides a very convenient way to check the beginning and end of a string . Similar operations can also be implemented using slices , But the code doesn't look that elegant . such as ：
'spam.txt' > filename[-4:] == '.txt' True > url = 'http://www.python.org' > url[:5] == 'http:' or url[:6] == 'https:' or url[:4] == 'ftp:' True >> filename =
You can also want to use regular expressions to implement , such as ：
'http://www.python.org' > re.match('http:jhttps:jftp:', url) <_sre.SRE_Match object at 0x101253098> >> import re > url =
This way will work , But for a simple match, it's a little bit like killing a chicken with a knife , The method in this section is more concise .
Last mention , When combined with other operations such as normal data aggregation startswith() and endswith() The method is very good . such as , The following statement checks whether the specified file type exists in a folder ：
if any(name.endswith(('.c', '.h')) for name in listdir(dirname)): ...