您在這裡

Parsing strings

23 二月, 2015 - 16:03

Often, we want to look into a string and find a substring. For example if we were presented a series of lines formatted as follows:

From stephen.marquard@ uct.ac.za Sat Jan 5 09:14:16 2008

And we wanted to pull out only the second half of the address (i.e. uct.ac.za) from each line. We can do this by using the find method and string slicing.

First, we will find the position of the at-sign in the string. Then we will find the position of the first space after the at-sign. And then we will use string slicing to extract the portion of the string which we are looking for.

>>> data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'>>> atpos = data.find('@')>>> print atpos21>>> sppos = data.find(' ',atpos)>>> print sppos31>>> host = data[atpos+1:sppos]>>> print hostuct.ac.za>>>

We use a version of the find method which allows us to specify a position in the string where we want find to start looking. When we slice, we extract the characters from “one beyond the at-sign through up to but not including the space character”.

The documentation for the find method is available at docs.python.org/library/string.html.