School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,119 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,959 people online right now. Registration is fast and FREE... Join Now!




Find string in a file

 

Find string in a file

agentkirb

16 Sep, 2009 - 07:27 AM
Post #1

D.I.C Head
**

Joined: 28 Dec, 2008
Posts: 164



Thanked: 1 times
My Contributions
I'm working on a simple program that does a few things:

-Opens a file
-Finds the number of times a string appears
-Finds the number of lines in the file

CODE


import os.path

file=input("enter the name of the file")
string=input("enter the string you want to find")

def number_string(file,string):
numline=0
numfound=0

infile = open(file,"r")
    while infile:
        line=infile.readline()
        if line.find(string) >= 0:
            numfound+=1
        s= line.split()
        n= len(s)
        if n==0:
                        numline+=1
            break
        
print "Number of lines", numline
print "Number of times ", string, " found ", numfound


This is what I have so far (I don't know if it's the current version, I'm on another computer atm). Is this the best way to find the number of instances of a string in a file? The idea is to use the split function until the length of your string = 0, then go to the next line. I'm new to this language so any help would be appreciated.

User is offlineProfile CardPM
+Quote Post


Nallo

RE: Find String In A File

16 Sep, 2009 - 01:26 PM
Post #2

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
Halv a dozen Thigs I dont like about your code:

1.)
file = ....

file is a keyword in python, but you use it as a variable rolleyes.gif

2.)
while infile: ....

infile is a file object, so it will always be true, you may as well write
while 1: ...

3.)
You test for an occurance in a line read, but not for multiple occurances in the same line.
You wanted that?

4.)
The identitation of your code seems wrong(at least of what you posted)

5.)
I dont understand your while loop ... seems not right, but I may be too tired to understand it.
Anyway, the way I would code it is (late at night that is):

CODE
my_file = ...
my_string = ...

infile = open(my_file,"r")

numlines = 0
found = 0
for line in infile:
    numlines += 1
    while 1:
        str_found_at = line.find(my_string)
        if str_found_at == -1:
            #string not found in line ...
            #go to next (ie break out of the while loop)
            break
        else:
            #string found in line
            found += 1
            #more than once in this line?
            #lets strip string and anything prior from line and
            #then go through the testing loop again
            line = line[str_found_at + len(string):]
infile.close()

print "%s was found %i times in %i lines", %string, %found, %numlines


edit:
even simpler:
CODE
my_file = ...
my_string = ...
infile = open(my_file,"r")

numlines = 0
found = 0
for line in infile:
    numlines += 1
    found += line.count(my_string)
infile.close()

print "%s was found %i times in %i lines", %string, %found, %numlines


This post has been edited by Nallo: 17 Sep, 2009 - 03:58 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:47PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month