2012-04-23 51 views
-4

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

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: 

------------------------------------------------ 
------------------------------------------------ 
------------------------------------------------ 
+2

“请为我写我的代码。”不是这个论坛的正确声音。它应该被解释为“我写这篇文章,它不工作,你能帮我吗?” – 2012-04-23 20:43:32

+0

对不起,我只是迷失在使用哪个脚本。我从来没有写过脚本,但是下次我会说清楚,我不希望我的代码被写成只是一个提示会很好。感谢您阅读我的问题。 – mshah 2012-04-23 20:50:41

回答

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)