当前位置:网站首页>Notes on Python cookbook 3rd (2.2): String start or end match

Notes on Python cookbook 3rd (2.2): String start or end match

2020-11-09 23:53:00 Giant ship

String start or end match

problem

You need to check the beginning or end of a string through the specified text pattern , For example, file name suffix , URL Scheme wait .

solution

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 :

>>> filename = 'spam.txt'
>>> filename.endswith('.txt')
True
>>> filename.startswith('file:')
False
>>> url = 'http://www.python.org'
>>> url.startswith('http:')
True
>>>

If you want to check multiple matches, maybe , Just put all the matches in a tuple , Then pass it to startswith() perhaps endswith() Method :

>>> import os
>>> filenames = os.listdir('.')
>>> 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
>>>

Another example

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 :

>>> choices = ['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
>>>

Discuss

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 :

>>> filename = 'spam.txt'
>>> filename[-4:] == '.txt'
True
>>> url = 'http://www.python.org'
>>> url[:5] == 'http:' or url[:6] == 'https:' or url[:4] == 'ftp:'
True
>>>

You can also want to use regular expressions to implement , such as :

>>> import re
>>> url = 'http://www.python.org'
>>> re.match('http:jhttps:jftp:', url)
<_sre.SRE_Match object at 0x101253098>
>>>

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)):
...

版权声明
本文为[Giant ship]所创,转载请带上原文链接,感谢