import file search

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 11
Reputation: Zelores is an unknown quantity at this point 
Solved Threads: 0
Zelores Zelores is offline Offline
Newbie Poster

import file search

 
0
  #1
Mar 18th, 2008
Hey guys,
I got another stupid question. For my last assignment, I have to take a .txt file that has contacts in it, import it into python (already did that) and use it to search with. the file goes
last name
first name
number # repeats about 4 more times.
the thing is, I dont know how to use the file to search a name or number. i have to put in a number or name and it comes back with all the info (first, last and number). How do I go about doing this?

i know the first part is
fileInput = open("entries.txt","r")
contacts = fileInput.read()

thx. also thanks for helping me on my last post too!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 948
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: import file search

 
0
  #2
Mar 18th, 2008
One approach would be to group the lines in the contacts string into a list of [first, last, number] lists ...
  1. # assume your contacts string looks like this after file read
  2. contacts = """\
  3. Adam
  4. Adler
  5. 12345
  6. Bart
  7. Johnson
  8. 23456
  9. Carl
  10. Lark
  11. 34567
  12. Don
  13. Herbst
  14. 45678
  15. """
  16.  
  17. # remove any trailing newline from the string
  18. contacts = contacts.rstrip('\n')
  19. # convert to a list of lines
  20. lines_list = contacts.split('\n')
  21.  
  22. print lines_list # test
  23.  
  24. # convert to a list of [first, last, number] lists
  25. contact_list = []
  26. temp = []
  27. # iterate in steps of 3
  28. for x in range(0, len(lines_list), 3):
  29. # form groups of 3
  30. temp.append(lines_list[x])
  31. temp.append(lines_list[x+1])
  32. temp.append(lines_list[x+2])
  33. contact_list.append(temp)
  34. # reset temp list
  35. temp = []
  36.  
  37. print contact_list # test
  38.  
  39. print '-'*50
  40.  
  41. # now add your search term
  42. search = "Don"
  43.  
  44. # and search the contact_list
  45. # may want to put that into a function
  46. # so you don't have to repeat the code for every search
  47. for item in contact_list:
  48. if search in item:
  49. # unpack the item
  50. first, last, number = item
  51. print "search =", search, " result =", first, last, number
  52.  
  53. # another search
  54. search = "23456"
  55.  
  56. for item in contact_list:
  57. if search in item:
  58. # unpack the item
  59. first, last, number = item
  60. print "search =", search, " result =", first, last, number
  61.  
  62. """
  63. my output -->
  64. ['Adam', 'Adler', '12345', 'Bart', 'Johnson', '23456', 'Carl', 'Lark', '34567', 'Don', 'Herbst', '45678']
  65. [['Adam', 'Adler', '12345'], ['Bart', 'Johnson', '23456'], ['Carl', 'Lark', '34567'], ['Don', 'Herbst', '45678']]
  66. --------------------------------------------------
  67. search = Don result = Don Herbst 45678
  68. search = 23456 result = Bart Johnson 23456
  69. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1353 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC