Search and Find through a list Python
Question: I have a main text file that looks like this:

    
    
    STATUS| CRN| SUBJECT| SECT| COURSE| CREDIT| INSTR.| BLDG/RM| DAY/TIME| FROM / TO| 
    OPEN| 43565| ACA6202| 10| Acting II| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43566| ACA6206| 10| Topics:Classical Drama/Cult II| 2.00| Jacobson, L| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43567| ACA6210| 10| Text II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43568| ACA6212| 10| Voice and Speech II| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43569| ACA6216| 10| Movement II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43570| ACA6220| 10| Alexander Technique II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43571| ACA6224| 10| Stage Combat II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 43572| ACA6228| 10| Practicum IV| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 
    OPEN| 44500| ACA6595| 10| Selected Topics| 1.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15| 

My code below gathers only the "SUBJECT" column and strips the numbers from
the string. So for example, the output from the top of the file would print
several "ACA"s.

    
    
    with open ("/Users/it/Desktop/Classbook/classAbrevs.txt", "r") as myfile:
    subsAndAbrevsMap = tuple(open("/Users/it/Desktop/Classbook/classAbrevs.txt", 'r'))
    
    with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
    masterSchedule = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))
    
    
    for masterline in masterSchedule:
        masterline.strip()
        masterSplitLine = masterline.split("|")
    
        if masterSplitLine[0] != "STATUS":
            subjectAbrev = ''.join([i for i in masterSplitLine[2] if not i.isdigit()])
    

I have another .txt file that looks like this:

    
    
    Academy for Classical Acting,ACA
    Accountancy,ACCY
    Africana Studies,AFST
    American Studies,AMST
    Anatomy & Regenerative Biology,ANAT
    Anthropology,ANTH
    Applied Science,APSC
    Arabic,ARAB
    Art/Art History,AH
    Art/Fine Arts,FA
    Astronomy,ASTR
    Biochemistry,BIOC
    Biological Sciences,BISC

In my code below, I check to see if the abbreviations(column 2) in my second
.txt equal the abbreviations generated from my first .txt document. If it is a
match I would like to append the full class name:

    
    
    #open 2nd .txt, strip and split
    for subsline in subsAndAbrevsMap:
                subsline.strip()
                subLineSplit = subsline.split(",")
                print "subLineSplit is: " + subsline[0]
    
                if subLineSplit[1] == subjectAbrev:
                    realSubjectName = subLineSplit[0]
                    print "The subject name for abrev " + subjectAbrev + " is " + realSubjectName
    

I want the output to print:

    
    
    "The subject name for abrev ACA is Academy for Classical Acting"
    

What am I doing wrong?


Answer: First of all, these are csv files, so use your `csv` module!

    
    
    # path to first file is ~/classes.csv
    # path to second file is ~/abbr.csv
    
    import csv
    
    with open("~/classes.csv", 'rU') as classes_csv,\
         open("~/abbr.csv", 'rU') as abbr_csv:
        classes = csv.reader(classes_csv, delimiter='|')
        abbr = csv.reader(abbr_csv, delimiter=',')
        header = next(classes)
    
        abbr_dict = {line[1].strip():line[0].strip() for line in abbr}
        # create a lookup dictionary for your tags -> names
    
        class_tags = (line[2].strip("0123456789 ") for line in classes)
        # create a genexp for all the extant tags in ~/classes.csv
    
        result = {tag:abbr_dict[tag] for tag in class_tags if tag in abbr_dict}
    

Then it should be easy to format your result.

    
    
    for abbr,cls in result.items():
        print("The abbreviation for {} is {}".format(cls,abbr))
    

