4 Replies - 11745 Views - Last Post: 19 May 2011 - 03:45 AM Rate Topic: -----

#1 robot_penguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 11

Search a text file in Python

Posted 18 May 2011 - 09:07 PM

I'm very new to Pyhton and programming in general and thought I'd try something I thought was simple. Basically I want to open a txt file and search it for numbers using an iteration loop sequence. It asks for a number but tells me it can't find it even though the number is in the txt file. Also, I'm sure about the loop process. I've tried a few different ways of doing it but the result is always the same. I apologize if the post is too long.

First set of code.
def main():
    #Create a bool variable to use as a flag.
    found = False
 
    #Get the search value.
    search = raw_input('Enter a number to search for: ')

    #Open the numbers.txt file.
    account_file = open('numbers.txt', 'r')
    
    #Read the first record.
    line = number_file.readline()

    #Determine whether this record matches the search value.
    if line == search:
        #Display the record.
        print 'Number:' , line
        #Set the found flag to True.
        found = True

    #Read the next line
    line = number_file.readline()

    #Close the file.
    number_file.close()

    #If the search value was not found in the file
    #display a message.
    if not found:
        print 'The number was not found.'
 
#Call the main function.
main()


Second set of code.
def main():
    #Open file for reading
    infile = open('numbers.txt', 'r')
    #Read the contents of the file into a list
    number = infile.readlines()
    #Convert each element into an int
    index = 0
    while index !=len(account_number):
        number = [int(num) for num in number]
        index +=1
    #Get an the search value
    search = raw_input('Enter a number to search for: ')
    #Determine whether this record matches the search value.
    if search in number:
        print 'Number found.', search
    else:
        print 'The number was not found.'
    #Close the file.
    infile.close()        

#Call the main function.
main()


If someone could point me in the right direction, it would be very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Search a text file in Python

#2 LiveToCode  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 37
  • Joined: 17-March 10

Re: Search a text file in Python

Posted 18 May 2011 - 09:17 PM

How is the data in the text file formatted?

EDIT:
For your first set of code, only the first two lines of the text file are read. As for your second set of code, what is account_number at line 8? I believe you meant len(number). Line 9 is also problematic. The variable number is over written, therefore, the content of the file is lost. Name your variable to something else. Set numbers = [] before the while loop. number is a list of line inputs of the text file. Change your line 9 to numbers += [int(num) for num in number[index]. Since numbers contains a list of integers and search is a STRING, you need to convert search to an integer simply by doing int(search) or int(raw_input(...)).

This post has been edited by LiveToCode: 18 May 2011 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 robot_penguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 11

Re: Search a text file in Python

Posted 18 May 2011 - 09:56 PM

Thanks for your response. Yes, that line was left over from another attempt with a different file. This file is formatted with 1, 7 digit number per line for 18 lines. I tried your changes and now get ValueError: invalid literal for int() with base 10: '' when I try to run the program.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Search a text file in Python

Posted 19 May 2011 - 03:37 AM

So what's the current code you're using?
Was This Post Helpful? 0
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Search a text file in Python

Posted 19 May 2011 - 03:45 AM

The big problem with both of your programs:

You said you wanted to loop over the lines of your file, but you don't actually do that in your code.
I expected something like the following in your code:
...
lines = infile.readlines()
for line in lines: #loop over the lines
    if search in line: #test if search is substring of line
        print "Found in line %line" %line
        found = True
...


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1