Search code examples
pythonfilelistformatabbreviation

Need to match string to line in file in Python


my first time asking something here. I have a text file of names, 1 name per line, that i'm reading in to a list, and then duplicating that list twice, the first time to remove \n's and a second time to lowercase the list. then I ask the user for a search term, and convert their input to lowercase, and then search the lowercase version of the list for that, then i get the index of the match, and use that to display the non-lowercase version of that list item back to the user (so they can type for example anivia and get back Anivia). This is working fine but I'm sure my code is pretty bad. What I would like to do is add specific abreviations for some of the names in the list file, and accept those abbreviations as input, but still display back the full name. For example, user enters "mumu" and it sees that the list has Amumu - mumu, to reference Amumu. How could I go about accepting that abreviation? Also other cases like mf for Miss Fortune or kha for Kha'Zix. I thought of maybe having a second file contain the list of abbreviations but that seems so wasteful and I'm sure theres a better way. Here is my bad code so far:

f = open("champions.txt") #open the file
list = f.readlines() #load each line into the list
#print list
list2 = [item.rstrip('\n') for item in list] #remove trailing newlines in copy list
list3 = [item.lower() for item in list2] #make it all lowercase

print "-------\n", list2 #print the list with no newlines just to check

print "which champ" #ask user for input
value = raw_input().lower() #get the input and make it lowercase
if value in list3: #check list for value and then print back a message using that index but from the non-lowercase list
    pos = list3.index(value)
    print "list contains", list2[pos]
else: #if the input doesn't match an item print an error message
    print "error"

ill put this all into some function in my main file once it's working the way i need. Basically I want to change some of the lines in my text file to have valid alternate abreviations and be able to accept those in this and still display the full name back to the user. For example, one of the lines in my secondary text file that has the abbreviations has a line of:

Kog'Maw - kogmaw, kog, km

how can i simplify what I have and add that functionality? I'm not really sure where to start, I'm pretty new to python and programming in general. Thank you for any help you can provide, sorry for such a long post.


Solution

  • OK, here's a revised answer that assumes there's one file containing names and abbreviations as shown at the beginning of this.

    Essentially what it does is make a large lookup table that maps any abbreviation in the file plus the name itself in lowercase to the name at the beginning of each line.

    lookup = {}
    with open("champions.txt") as f:
        for line in f:
            line = line.rstrip().split('-', 1)
            if not line: continue # skip any blank lines
    
            name = line[0].strip()
            lookup[name.lower()] = name
            if len(line) == 2:  # any alternative names given?
                for item in line[1].split(','):
                    lookup[item.strip()] = name
    
    print 'lookup table:'
    for alt_name, real_name in sorted(lookup.items()):
        print '{}: {}'.format(alt_name, real_name)
    print
    
    while True:
        print "which champ (Enter to quit): "  # ask user for input
        value = raw_input().lower()  # get the input and make it lowercase
        if not value: break
    
        real_name = lookup.get(value)
        if real_name:
            print 'found:', value, '-->', real_name
        else:
            print 'error: no match for', value