import csv
with open('test.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter = collections.defaultdict(int) for row in data: counter[row[1]] += 1 for row in data: if counter[row[1]] >= 4: writer = csv.writer(open("test1.csv", "wb")) writer.writerows(row)
我得到奇怪的输出!此代码有什么问题?
用途csv.writer:
csv.writer
import csv with open('thefile.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter = collections.defaultdict(int) for row in data: counter[row[0]] += 1 writer = csv.writer(open("/path/to/my/csv/file", 'w')) for row in data: if counter[row[0]] >= 4: writer.writerow(row)