searching through a string and pulling characters

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Aug 18 17:54:36 EDT 2008


En Mon, 18 Aug 2008 17:40:13 -0300, Alexnb <alexnbryan at gmail.com> escribió:

> Lets say I have a text file. The contents look like this, only there is A
> LOT of the same thing.
>
> () A registry mark given by underwriters (as at Lloyd's) to ships in
> first-class condition. Inferior grades are indicated by A 2 and A 3.
> () The first three letters of the alphabet, used for the whole alphabet.
> () In church or chapel style; -- said of compositions sung in the old church
> style, without instrumental accompaniment; as, a mass a capella, i. e., a
> mass purely vocal.
> () Astride; with a part on each side; -- used specif. in designating the
> position of an army with the wings separated by some line of demarcation, as
> a river or road.
>
> Now, I am talking 1000's of these. I need to do something like this. I will
> have a number, and what I want to do is go through this text file, just like
> the example. The trick is this, those "()'s" are what I need to match, so if
> the number is 245 I need to find the 245th () and then get the all the text
> from after it until the next (). If you have an idea about the best way to
> do this I would love your help. If you made it all the way through thanks!
> ;)

py> data = """() A registry mark given by underwriters (as at Lloyd's) to ships in
... first-class condition. Inferior grades are indicated by A 2 and A 3.
... () The first three letters of the alphabet, used for the whole alphabet.
... () In church or chapel style; -- said of compositions sung in the old church
... style, without instrumental accompaniment; as, a mass acapella, i. e., a
... mass purely vocal.
... () Astride; with a part on each side; -- used specif. in designating the
... position of an army with the wings separated by some line of demarcation, as
... a river or road.
... """
py> # there is no 0th element, I presume, so I start with an empty one
... current = []
py> checks = [current]
py> for line in data.split('\n'):
...   if line[:2]=='()':
...     current = [line[3:]]
...     checks.append(current)
...   else:
...     current.append(line)
...
py> print checks[3]
['In church or chapel style; -- said of compositions sung in
 the old church', 'style, without instrumental accompaniment
; as, a mass a capella, i. e., a', 'mass purely vocal.']

This reads the whole file, assuming you want more than one item at a time. Note that you get a list of lines for each item - you may join them into a long string using 
longline = ' '.join(lines)

-- 
Gabriel Genellina




More information about the Python-list mailing list