2013-06-11 48 views
0

我試圖根據csv的列中的值是否爲某個字符串來編寫條件。如何在Python中檢查電子表格中列的值

這是我的代碼,在那裏我將執行基於列「類型」單元格的內容是否爲「問題」的一些東西:

f = open('/Users/samuelfinegold/Documents/harvard/edXresearch/snaCreationFiles/time_series/time_series.csv','rU') 
reader = csv.DictReader(f, delimiter=',') 

for line in reader: 
    if line['type'] == 'Question': 
     print "T" 

CSV:

錯誤我越來越:AttributeError: DictReader instance has no attribute '__getitem__'

post_id thread_id author_id post_content types  time  votes_up votes_down posters 
1   0   Jan  NULL   Question 3/1/12 10:45 5 1 Jan, Janet, Jack 
2   0   Janet NULL   Answer  3/1/12 11:00 2 1 Jan, Janet, Jack 
3   0   Jack NULL   Comment  3/2/12 8:00 0 0 Jan, Janet, Jack 
4   1   Jason NULL   Question 3/4/12 9:00 3 1 Jason, Jan, Janet 
5   1   Jan  NULL   Answer  3/7/12 1:00 3 1 Jason, Jan, Janet 
6   1   Janet NULL   Answer  3/7/12 2:00 1 2 Jason, Jan, Janet 
+0

發表更多代碼請 – oleg

+0

確定這是導致問題的代碼?如果你已經完成了'reader''type']'而不是'line ['type']',那麼你會得到這個錯誤。添加'print type(line),repr(line)'來看看它是什麼。 – DSM

+0

當我在閱讀器中寫入'line:print type(line)'時,它什麼也不返回 – goldisfine

回答

2

我把你放在一個逗號分隔的CSV文件中提供的數據的標題行,然後我在您提供的數據上運行您的代碼,並獲得KeyError,type,因此我將if line['type']更改爲if line['types'],它的工作。

我的代碼:

import csv 

f = open('test.csv','rU') 
reader = csv.DictReader(f,delimiter=',') 

for line in reader: 
    print line 
    if line['types'] == 'Question': 
     print 'The above line has type question' 

我的輸出:

{'thread_id': '0', 'posters ': 'Jan', None: ['Janet', 'Jack'], 'post_id': '1', 'post_content': 'NULL', 'time': '3/1/12 10:45', 'votes_down': '1', 'votes_up': '5', 'author_id': 'Jan', 'types': 'Question'} 
The above line has type question 
{'thread_id': '0', 'posters ': 'Jan', None: ['Janet', 'Jack'], 'post_id': '2', 'post_content': 'NULL', 'time': '3/1/12 11:00', 'votes_down': '1', 'votes_up': '2', 'author_id': 'Janet', 'types': 'Answer'} 
{'thread_id': '0', 'posters ': 'Jan', None: ['Janet', 'Jack'], 'post_id': '3', 'post_content': 'NULL', 'time': '3/2/12 8:00', 'votes_down': '0', 'votes_up': '0', 'author_id': 'Jack', 'types': 'Comment'} 
{'thread_id': '1', 'posters ': 'Jason', None: ['Jan', 'Janet'], 'post_id': '4', 'post_content': 'NULL', 'time': '3/4/12 9:00', 'votes_down': '1', 'votes_up': '3', 'author_id': 'Jason', 'types': 'Question'} 
The above line has type question 
{'thread_id': '1', 'posters ': 'Jason', None: ['Jan', 'Janet'], 'post_id': '5', 'post_content': 'NULL', 'time': '3/7/12 1:00', 'votes_down': '1', 'votes_up': '3', 'author_id': 'Jan', 'types': 'Answer'} 
{'thread_id': '1', 'posters ': 'Jason', None: ['Jan', 'Janet'], 'post_id': '6', 'post_content': 'NULL', 'time': '3/7/12 2:00', 'votes_down': '2', 'votes_up': '1', 'author_id': 'Janet', 'types': 'Answer'} 

您有一個名爲None關鍵是因爲在海報欄的數據已經逗號分隔的原因,因此,只有拳頭值在列將被分配的關鍵'海報'

我還不確定你爲什麼得到一個attribute error,但一個簡單的cha nge到你的代碼它爲我工作。

0

也許你應該檢查一下你的數據具有通過

has_header(sample) 
+0

我不認爲這是問題 – goldisfine

相關問題