从csv文件中识别和计算python中的不同输出

时间:2018-06-01 08:10:38

标签: python

我从csv文件中提取数据,并从每行数据中打印出三个字母的国家/地区代码。如何让python从输出的数据中识别每个唯一国家代码的出现次数?以下是我打印国家/地区代码的内容。

import csv

with open('2017CountryData.csv') as csvfile:

  readCSV = csv.reader(csvfile, delimiter=',')
  for row in readCSV:
        countries = row[1]
        print(countries)

1 个答案:

答案 0 :(得分:0)

使用collections.Counter

import csv

with open('2017CountryData.csv') as csvfile:
    countries = []
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row[1] not in countries:
            countries.append(row[1])

print(Counter(countries))