擅长:python、mysql、java
<p>把它放在一起csv.reader用法):</p>
<pre><code>import csv
import collections
d = collections.defaultdict(int)
with open("names.csv", "rb") as f: # Python 3.x: use newline="" instead of "rb"
reader = csv.reader(f):
reader.next() # ignore useless heading row
for row in reader:
for name in row:
name = name.strip()
if name:
d[name] += 1
morethan3 = [(name, count) for name, count in d.iteritems() if count >= 3]
morethan3.sort(key=lambda x: x[1], reverse=True)
for name, count in morethan3:
print name, count
</code></pre>
<p><strong>更新</strong>回应评论:</p>
<p>无论您是否使用DictReader方法,您都需要通读整个CSV文件。如果您想忽略“name2”列(<em>not row</em>),则忽略它。您不需要按照变量名“rows”的建议保存所有数据。下面是一个更通用的方法的代码,它不依赖列标题的特定顺序,并且允许选择/拒绝特定的列。在</p>
^{pr2}$