python - 使用DictReader查找特定的行属性

时间:2016-03-22 11:47:53

标签: python csv

我想用DictReader找到特定的行。

csv文件(test.csv)

getSnippet = function(keywords, fullText) {
  var keywordCount = keywords.length,
      keywordIndexes = [];

  // Find each occurrence of every word
  for(var i=0; i < keywordCount; i++) {
    var searchPos = 0;
    var word = keywords[i];
    var index = -1;
    do {
      index = fullText.indexOf(keywords[i],searchPos);
      if (index >= 0) {
        keywordIndexes.push({i:index, word:word});
      }
      searchPos = index + 1;
    } while (index >= 0);
  }

  keywordIndexes.sort(function(a, b) { return a.i == b.i ? 0 : a.i < b.i ? -1 : 1; });

  // Find the shortest run by starting at each array index and scanning to the
  // right until we have encountered each word in the list.
  for (i=0, n=keywordIndexes.length-keywordCount; i<=n; i++) {
    // NOTE: We actually can actually stop once there are fewer keyword
    // indexes than keywords, since we know we won't find all the keywords (hence the subtraction of keywordCount)
    var foundWords = {},
        foundCount = 0;
    snippetStart = keywordIndexes[i].i;

    for (j=i; j < keywordIndexes.length; j++) {
      var word = keywordIndexes[j].word;
      if (!foundWords[word]) {
        foundWords[word] = true;
        foundCount++;
      }
      if (foundCount == keywordCount) {
        // We've found all the words
        snippetEnd = keywordIndexes[j].i + word.length;
        if (minSnippet.end - minSnippet.start > snippetEnd - snippetStart) {
          minSnippet.end = snippetEnd;
          minSnippet.start = snippetStart;
        }
        break;
      }
    }
  }
  return fullText.substring(minSnippet.start, minSnippet.end);
}

.py文件

numb, firstname, lastname
1, yong, kim
2, gwang, lee
3, geun, lee 
...

修改

当我运行此代码时,会出现以下错误。

import csv
from collections import defaultdict
from operator import itemgetter

result = {}
result = defaultdict(lambda: 0, result)

tmp = open('test.csv', 'r')
file = csv.DictReader(tmp)

# what I want
print file[1]['firstname'] # => I want it to be : "gwang"

我该怎么办?

喝彩!

2 个答案:

答案 0 :(得分:3)

作为it is said on the documentation

  

返回一个reader对象,它将迭代给定csvfile中的行。

您无法使用reader_obj[x]__getitem__)访问行,因为它是迭代器。

首先必须将迭代器实现为列表:

tmp = open('test.csv', 'r')
file = csv.DictReader(tmp)
data = list(file)

print data[1]['firstname']

这当然是将整个文件读取到RAM,如果它是一个大文件可能并不理想。

如文档中所示,最好只是遍历阅读器。

答案 1 :(得分:2)

您需要遍历您创建的DictReader对象,即file。 请参阅此示例以了解如何使用DictReader。基本上你是逐行逐步csv

From the docs:

import csv
with open('names.csv') as csvfile:
     reader = csv.DictReader(csvfile)
     for row in reader:
         print(row['first_name'], row['last_name'])

因此,只需使用if检查您所使用的当前row是否与您的搜索criterea匹配