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!!

One approach would be to group the lines in the contacts string into a list of [first, last, number] lists ...

# 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
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.