By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,210 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,210 IT Pros & Developers. It's quick & easy.

Re: searching through a string and pulling characters

P: n/a
En Mon, 18 Aug 2008 17:40:13 -0300, Alexnb <al********@gmail.comescribió:
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!
;)
pydata = """() 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 = []
pychecks = [current]
pyfor line in data.split('\n'):
.... if line[:2]=='()':
.... current = [line[3:]]
.... checks.append(current)
.... else:
.... current.append(line)
....
pyprint 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

Aug 18 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.