我们有许多csv文件如下:
Name,Type
1,Fuji
2,Fuji
3,Fuji
4,Fuji
5,Washington
6,Washington
7,Washington
8,Washington
9,Washington我们会打印出不同类型的苹果而不打印复制件。
Fuji:6 Washington:4 Gaza:1或
Fuji Washington Gaza 以下是我们的尝试。尽管它似乎不起作用,原因不明。
# Python 2.7
import csv
import glob
import collections
from collections import Counter
list = glob.glob('C:Apple*.csv')
for file in list:
infile = open(file, "rb")
reader = csv.reader(infile)
for column in reader:
Discipline = column[1]
print collections.Counter(Discipline)
infile.close()发布于 2014-06-17 23:15:25
我以前没有使用过csv模块,但下面是我认为您可能试图实现的快速尝试。
import csv
src = r'C:\apples_before.csv'
dst = r'C:\apples_after.csv'
apples = set([])
# Read file.
with open(src, 'r') as srcfile:
reader = csv.reader(srcfile, delimiter=',')
for index, row in enumerate(reader):
if index == 0:
continue
apples.add(row[1])
# Write file.
# @warning: Please note that I am making an assumption in terms of the number
# component. I am assuming it is a row number.
with open(dst, 'w') as dstfile:
writer = csv.writer(dstfile, delimiter=',')
for index, apple in enumerate(apples):
if index == 0:
writer.writerow(['Name', 'Type'])
writer.writerow([index + 1, apple])https://stackoverflow.com/questions/24274359
复制相似问题