通过列表Python搜索和查找

时间:2014-12-31 22:07:38

标签: python list iteration tuples

我有一个主文本文件,如下所示:



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| 




我的代码下面只收集" SUBJECT"列并从字符串中删除数字。因此,例如,文件顶部的输出将打印几个" 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()])

我有另一个.txt文件,如下所示:



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




在下面的代码中,我检查我的第二个.txt中的缩写(第2列)是否等于从我的第一个.txt文档生成的缩写。如果是匹配,我想附加完整的班级名称:

#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

我希望打印输出:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,这些是csv文件,因此请使用您的csv模块!

# 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}

然后很容易格式化你的结果。

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