3 Replies - 461 Views - Last Post: 15 June 2013 - 12:28 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Searching word in text file

Posted 15 June 2013 - 07:00 AM

I am not good with I/O text, I am writing a program where the user will search a word and display only specific results for them. However the problem is, I am trying to figure out if there is a way to append in a 2D array and allow the user to search.

test.txt
ByWard Blue Inn|Ottawa, Canada|2|Standard Queen Room|10/06/2013|13/06/2013|1075
Simple Hotel|Toronto, Canada|4|Two-Bedroom Suite|10/06/2013|13/06/2013|1540


program.py
class Hotel:
    def __init__(self, h_name = "no data", h_location = "no data", r_type = "no data",
    n_rooms = "no data", c_in = "no data", c_out = "no data", h_price="no data"):
        self.h_name = h_name
        self.h_location = h_location
        self.r_type = r_type
        self.c_in = c_in
        self.c_out = c_out
        self.n_rooms = n_rooms
        self.h_price = h_price

    def search(self, word):
        text_file = open("test.txt", "r")
        for line in text_file:
            if (word == line):
                print(line)

new_obj = Hotel()
new_obj.search("ByWard Blue Inn)



The reason why I am trying to add in a 2D array it is so that I can ask the user if they want to search other keywords include: location and room type.

Is there any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Searching word in text file

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Searching word in text file

Posted 15 June 2013 - 09:19 AM

This kinda sounds like a good time to use tuples. You can create tuples and follow a convention like:
(hotelName, location, roomType,)

And then you'll know that location is always the second element, and roomType is always the third. This would let you check a search term against the corresponding type fairly easily.

As for doing searches, toFind == word isn't the ideal way. It will get messed up on uppercase vs lowercase and it looks for an exact word. for example, if I typed in Simple, it wouldn't find Simple Hotel. One way to improve it might be something like
if toFind.lower() in word.lower():
Of course, there are many better ways you could use, but this one maintains simplicity and is in line with what you originally were doing.
Was This Post Helpful? 1
  • +
  • -

#3 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Searching word in text file

Posted 15 June 2013 - 09:47 AM

Oh I definitely forgot that Python is case sensitive and would search for exact words. In addition I was able to store in a 2D array, by declaring a 1D array as, I need to create a function that find the number of the lines and use the number of the lines to literate through my file by appending to array.

So, I still got to find a way to search the words.
Was This Post Helpful? 0
  • +
  • -

#4 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Searching word in text file

Posted 15 June 2013 - 12:28 PM

Well, I solved my own problems... After 2 hours tracing through the logic, I finally figure out how to do it.

So my problem is solved :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1