-4

我需要它帮助 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:

------------------------------------------------
------------------------------------------------
------------------------------------------------
4

1 回答 1

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)
于 2012-04-23T20:45:50.923 回答