我正在尝试根据 csv 列中的值是否是某个字符串来编写条件。
这是我的代码,我将根据“类型”列中单元格的内容是否为“问题”来执行一些操作:
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