searching for the number of occurences of a string

M

M.N.A.Smadi

hi;
say i have a text file with a string ( say '(XYZ)') and I want to find
the number of line where this string has occured. What is the best way
to do that?

what about if that string was say a 0 with leading and trailing white
spaces, would that be any different?

thanks
moe smadi
 
J

James Stroud

M.N.A.Smadi said:
hi;
say i have a text file with a string ( say '(XYZ)') and I want to find
the number of line where this string has occured. What is the best way
to do that?

what about if that string was say a 0 with leading and trailing white
spaces, would that be any different?

thanks
moe smadi

Some pieces:

To read every line in a file:

afile = open(filename)
for aline in afile:
do_something_here()

To see if a string contains another string, try

if another_string() in astring:
do_something_else_here()

If you need regular expressions, see the re module.

For instance:

import re
regex = re.compile(r'(?:\s+0\s+)|\(XYZ\)')

print regex.search(' 0 ').group()
print regex.search(' (XYZ) abc').group()

if regex.search('abcdefg'):
print 'yep'
else:
print 'nope'

That should be everything you could possibly need. Its up to you to put
it all together.

James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

James Stroud

M.N.A.Smadi said:
hi;
say i have a text file with a string ( say '(XYZ)') and I want to find
the number of line where this string has occured. What is the best way
to do that?

what about if that string was say a 0 with leading and trailing white
spaces, would that be any different?

thanks
moe smadi


To read every line in a file:

afile = open(filename)
for aline in afile:
do_something_here()
afile.close()

To see if a string contains another string, try

if another_string in astring:
do_something_else_here()

If you need regular expressions, see the re module.

For instance:

import re
regex = re.compile(r'(?:\s+0\s+)|\(XYZ\)')

print regex.search(' 0 ').group()
print regex.search(' (XYZ) abc').group()

if regex.search('abcdefg'):
print 'yep'
else:
print 'nope'

That should be everything you could possibly need. Its up to you to put
it all together.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
M

marek.rocki

hi;
say i have a text file with a string ( say '(XYZ)') and I want to find
the number of line where this string has occured. What is the best way
to do that?

I would suggest:

# Read file contents
lines = file('filename.txt').readlines()
# Extract first line number containing 'XYZ' string
[(i, l) for (i, l) in enumerate(lines) if 'XYZ' in l][0][0]

Best regards,
Marek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top