【问题标题】:How to check value of column in spreadsheet in Python如何在 Python 中检查电子表格中列的值
【发布时间】:2013-06-11 14:21:29
【问题描述】:

我正在尝试根据 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

【问题讨论】:

  • 请发布更多代码
  • 您确定是导致问题的代码吗?如果您使用reader['type'] 而不是line['type'],这就是您会得到的错误。添加print type(line), repr(line) 看看它到底是什么。
  • 当我写 for line in reader: print type(line) 它什么也不返回
  • 你在哪里做的? (请记住,您只能对 reader 进行一次 for 循环 - 之后,您就结束了。)只需 f = open("/Users//etc")reader = csv.DictReader(f, delimiter=',')for line in reader: print type(line), repr(line)。只有这四行。
  • 您能否提供一个重现问题的 csv 文件样本?

标签: python csv indexing


【解决方案1】:

我把你提供的数据放在一个逗号分隔的 CSV 文件中,然后我在你提供的数据上运行你的代码,得到一个 typeKeyError,所以我将 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,但只需对您的代码进行简单更改,它就对我有用。

【讨论】:

    【解决方案2】:

    python 在标准库中有一个处理 csv 文件的模块

    https://www.google.com/search?q=python+csv

    第一次命中:

    http://docs.python.org/library/csv.html

    【讨论】:

      【解决方案3】:

      也许你应该检查你的数据是否有标题行

      has_header(sample)
      

      【讨论】:

      • 我不认为这是问题
      猜你喜欢
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多