2012-04-23 52 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)