| | |
import file search
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
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!!
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!!
One approach would be to group the lines in the contacts string into a list of [first, last, number] lists ...
python Syntax (Toggle Plain Text)
# assume your contacts string looks like this after file read contacts = """\ Adam Adler 12345 Bart Johnson 23456 Carl Lark 34567 Don Herbst 45678 """ # remove any trailing newline from the string contacts = contacts.rstrip('\n') # convert to a list of lines lines_list = contacts.split('\n') print lines_list # test # convert to a list of [first, last, number] lists contact_list = [] temp = [] # iterate in steps of 3 for x in range(0, len(lines_list), 3): # form groups of 3 temp.append(lines_list[x]) temp.append(lines_list[x+1]) temp.append(lines_list[x+2]) contact_list.append(temp) # reset temp list temp = [] print contact_list # test print '-'*50 # now add your search term search = "Don" # and search the contact_list # may want to put that into a function # so you don't have to repeat the code for every search for item in contact_list: if search in item: # unpack the item first, last, number = item print "search =", search, " result =", first, last, number # another search search = "23456" for item in contact_list: if search in item: # unpack the item first, last, number = item print "search =", search, " result =", first, last, number """ my output --> ['Adam', 'Adler', '12345', 'Bart', 'Johnson', '23456', 'Carl', 'Lark', '34567', 'Don', 'Herbst', '45678'] [['Adam', 'Adler', '12345'], ['Bart', 'Johnson', '23456'], ['Carl', 'Lark', '34567'], ['Don', 'Herbst', '45678']] -------------------------------------------------- search = Don result = Don Herbst 45678 search = 23456 result = Bart Johnson 23456 """
May 'the Google' be with you!
![]() |
Similar Threads
- Help and support and Msconfig won't open, Search disabled (Windows NT / 2000 / XP)
- php drop down menu to search multiple sql tables (PHP)
- Substring Search (Python)
- spotlight doesn't search hypercard stacks (OS X)
- Search Method help needed!!! (Java)
- Please help with a script (Visual Basic 4 / 5 / 6)
- How to export excel data to a search engine? (MS Access and FileMaker Pro)
Other Threads in the Python Forum
- Previous Thread: Calculate string into answer
- Next Thread: RE: word count programming for python (wc)
Views: 1353 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






