473,699 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

searching for the number of occurences of a string

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
Mar 6 '06 #1
3 1596
M.N.A.Smadi wrote:
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_he re()

To see if a string contains another string, try

if another_string( ) in astring:
do_something_el se_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('a bcdefg'):
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/
Mar 6 '06 #2
M.N.A.Smadi wrote:
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_he re()
afile.close()

To see if a string contains another string, try

if another_string in astring:
do_something_el se_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('a bcdefg'):
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/
Mar 6 '06 #3
> 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

Mar 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3271
by: Stewart Allen | last post by:
Hi there I'm trying to find part serial numbers between 2 numbers. The user selects a part number from a combo box and then enters a range of serial numbers into 2 text boxes and the resulting query should find every machine that has that part number between the serial number range. The problem is that the serial number stored is a text field and the results are not what they should be.
33
2498
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following criteria: I have a list of about 100 numbers which correspond to the ID field and also another 40 say numbers corresponding to the numbers in the PRODUCT field. I want to show the rows that correspond to both these criteria.
4
3375
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the files could be upwards of 25mb in size and time is important. I don't want to take the route of loading the text of the file into a giant string and searching it, but would rather focus on a performance-minded solution. Any sugesstions for a...
7
1692
by: shubharamaswamy | last post by:
Reading standard input and printing the number of occurences of each letter in graphic format as follows: a: ..... b: .. . . . z: ... where no. of dots= no.of occurences
2
1964
by: kasala | last post by:
I get an xml document as input from other department. The input xml document i recieve has a particular word "rnx" which should not be there and my system doesn't support it. And there is also attribute xml:lang="EN" in some elements which should not be there. I am enclosing the code for reference. I used to remove the occurences manually...but now I need an assembly which can do that for me in C#.I have to remove the occurences of the word...
5
6926
by: Larry | last post by:
Dear all, I'm new to Python. I have a file (an image file actually) that I need to read pixel by pixel. It's an 8-bit integer type. I need to get the statistics like mean, standard deviation, etc., which I know a little bit already from reading numpy module. What I want to know is how to get the number of occurences of numeric element in an array. Say, b = array(()
1
4123
ajhayes
by: ajhayes | last post by:
I posted a few weeks ago about trying to set up a way to order by unit number (http://bytes.com/topic/access/answers/864507-need-help-setting-up-table-structure-ordering-unit) I'm trying to use a similar structure to what OldBirdman suggested in my previous post, but I'm a bit stuck. Here is what I've set up: tbl_Orders Order_Number Order_Date
3
2474
by: SLauren | last post by:
Hi, I am trying to find the records based on a search string which can be anything and can match any of the varchar fields in the table. Right now i am getting those records by using the combinations of "LIKE" and "OR" operators.But as per the requirements i also need to show the number of occurences of the search string that could match different varchar fields in a record?? Can anyone please suggest me how can i prepare this query?? ...
3
3562
by: lilly07 | last post by:
I am trying open a text file, and find the occurence of first column string (ie) $v in a separate file which contains only one column. my code is as follows but somehow it doesn't seem to count the number of occurences in the second file. #!/usr/bin/perl -w $datafile = $ARGV; print "$datafile\n"; open (LIST1, $datafile) || die "File not found\n";
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.