我試圖根據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
發表更多代碼請 – oleg
確定這是導致問題的代碼?如果你已經完成了'reader''type']'而不是'line ['type']',那麼你會得到這個錯誤。添加'print type(line),repr(line)'來看看它是什麼。 – DSM
當我在閱讀器中寫入'line:print type(line)'時,它什麼也不返回 – goldisfine