我需要帮助使用任何脚本解析文本

时间:2012-04-23 20:36:38

标签: python parsing shell csv

我需要它帮助shell脚本,python脚本或任何可以做到这一点的东西。我想创建一个脚本,可以将以下信息从“txt”文件解析为csv。我需要解析的在线成绩簿的信息是获取用户名和实验室分数。实验室得分可以在这一行找到

Your score for this lab: 20/20

并且可以在此行找到用户名

Student: username0

感谢您阅读并帮助我!

以下是文件test.txt

的示例
Student: username0

Your score for this lab: 20/20

Score Breakdown:
info...

Part 1:


Part 2:


Part 3:

------------------------------------------------
------------------------------------------------
------------------------------------------------
Student: username1

Your score for this lab: 20/20

Score Breakdown:
info...


Part 1:


Part 2:


Part 3:

------------------------------------------------
------------------------------------------------
------------------------------------------------
Student: username2

Your score for this lab: 20/20

Score Breakdown:



Part 1:


Part 2:


Part 3:

------------------------------------------------
------------------------------------------------
------------------------------------------------

1 个答案:

答案 0 :(得分:0)

呸,这在Python中很容易做到,为什么不与你分享呢?

def filtered(lines):
    for line in lines:
        if line.startswith('Student:') or line.startswith('Your score for this lab:'):
            yield line.rstrip().split()[-1]

with open('test.txt', 'r') as f:
    for student, score in zip(*[filtered(f)]*2):
        print(student, score)