searching through a string and pulling characters
news1234 at free.fr
Mon Aug 18 23:54:21 CEST 2008
Is it thousands of lines or millions of lines?
If it's just a few thousands and you're not working on an embedded
device with little memory you could use the brute force approach
Just read the whole file in one string vaiable
split everything into an array separated by '()'
Now you can directly access each element by number (or by number + 1 if
you want, that the first index is 0
Example: ( assuming your text file is bla.txt )
in_file = open('bla.txt')
whole_text = in_file.read()
sections = whole_text.split('()')
# now print all entries
for section in sections:
print '%3d' % n,'---------------'
n += 1
#or directly selecting one entry
print "=" * 60
print 'Entry %d\n' % n,sections
print "=" * 60
> This is similar to my last post, but a little different. Here is what I would
> like to do.
> 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!
More information about the Python-list