2013-08-20 94 views
3

我正在做一個基本的程序,有多選答案的測驗。我想從.DAT文件訪問數據。 以下是.DAT文件的基本佈局。在Python中讀取.DAT文件?

Which sport uses the term LOVE ? 
Tennis 
Golf 
Football 
Swimming 
A 

如何分別訪問每條線?

+0

[讀取二進制.dat文件作爲數組]可能的副本(https://stackoverflow.com/questions/11798800/reading-a-binary-dat-file-as-an-array) –

回答

7
for line in open(filename, 'r'): 
    item = line.rstrip() # strip off newline and any other trailing whitespace 
    ... 

對於獎金:網球!

+1

對不起,對python來說很新鮮。 'r'是做什麼的?謝謝 –

+0

'r'告訴'open'你想打開文件爲「read」而不是寫入,讀取二進制文件等。參見http://docs.python.org/2/library/functions.html#open – mattexx

+0

好的,非常感謝,非常感謝! –