从文本文件中提取信息时出现逻辑错误

时间:2014-04-27 22:38:00

标签: python python-3.x

我有一个格式为

的文本文件
356693

2013 46,  2013 67,  2013 47,  2013 63,  2013 59,  2013 76

Course Codes: MECN1001, MECN1003, CHEM1033, MATH1014, PHYS1014, PHYS1015

522021

2009 45,  2009 49,  2009 67,  2009 54,  2009 67,  2009 83,  2010 46,  2010 91,  2010 42,  2010 60,  2010 52,  2011 61,  2011 65,  2011 56,  2012 53,  2012 54,  2012 45,  2012 45,  2012 43,  2012 63,  2013 66,  2013 62,  2013 50,  2013 83,  2013 69,  2013 74,  2013 100

Course Codes: MECN1001, MECN1003, CHEM1033, MATH1014, PHYS1014, PHYS1015, MECN1001, MECN1003, MECN2011, ELEN2000, MATH2011, MECN1001, MECN2006, MECN2011, MECN2000, MECN2005, MECN2010, MECN2012, MECN2013, MECN2014, MECN2010, MECN2012, MECN2013, MECN3002, MECN3010, MECN3028, MATH3026

605417

2013 69,  2009 65,  2009 58

Course Codes: MECN1001, MECN1003, CHEM1033

现在我想只提取课程代码行并将它们存储在列表中。我遇到的问题是我的代码只打印最后一行。这是我的代码:

with open("Test.txt", "r") as textfile:
    for line in textfile:
        if len(line.strip()) > 6:
            Courses = line.split()
    print(Courses)  

1 个答案:

答案 0 :(得分:0)

您需要将结果存储在某处,并在相关行的开头说明Course Codes:字符串。尝试这样的事情:

prefix = 'Course Codes:'
courses = []
with open("Test.txt") as textfile:
    for line in textfile:
        if line.startswith(prefix):
            codes = line[len(prefix) + 1:]
            courses += codes.split(',')  # build in some flexibility in case a space is missing
    else:  # after the for-loop finishes
        courses = [course.strip() for course in courses]  # now remove spaces
    print(courses)

['MECN1001', 'MECN1003', 'CHEM1033', 'MATH1014', 'PHYS1014', 'PHYS1015', 'MECN1001', 'MECN1003', 'CHEM1033', 'MATH1014', 'PHYS1014', 'PHYS1015', 'MECN1001', 'MECN1003', 'MECN2011', 'ELEN2000', 'MATH2011', 'MECN1001', 'MECN2006', 'MECN2011', 'MECN2000', 'MECN2005', 'MECN2010', 'MECN2012', 'MECN2013', 'MECN2014', 'MECN2010', 'MECN2012', 'MECN2013', 'MECN3002', 'MECN3010', 'MECN3028', 'MATH3026', 'MECN1001', 'MECN1003', 'CHEM1033']