根据用户搜索总结csv文件中的列

时间:2017-06-09 19:24:24

标签: python-2.7 csv

我有以下csv文件:

data.cvs

school,students,teachers,subs
us-school1,10,2,0
us-school2,20,4,2
uk-school1,10,2,0
de-school1,10,3,1
de-school1,15,3,3

我正在尝试用户搜索学校所在国家/地区(我们或英国或德) 然后总结相应的列。 (例如,我们所有学生的总和 - *等) 到目前为止,我能够使用raw_input进行搜索并显示与该国家相对应的列内容,感谢有人能给我一些关于如何实现这一点的指示。

期望的输出:

国家:我们

学生总数:30

教师总数:6

总潜艇:2

-

import csv
import re
search = raw_input('Enter school (e.g. us: ')
with open('data.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
       school = row['school']
       students = row['students']
       teachers = row['teachers']
       sub = row['subs']

       if re.match(search, schools) is not None:
             print students

2 个答案:

答案 0 :(得分:1)

这相对容易做到 - 您只需要将您所在国家/地区分组,然后将所有值加在一起:

import collections
import csv

result = {}  # store the results
with open("data.csv", "rb") as f:  # open our file
    reader = csv.DictReader(f)  # use csv.DictReader for convenience
    for row in reader:
        country = row.pop("school")[:2]  # get our country
        result[country] = result.get(country, collections.defaultdict(int))  # country group
        for column in row:  # loop through all other columns
            result[country][column] += int(row[column])  # add them together

# Now you can use or print your result by country:
for country in result:
    print("Country: {}".format(country))
    print("Total students: {}".format(result[country].get("students", 0)))
    print("Total teachers: {}".format(result[country].get("teachers", 0)))
    print("Total subs: {}\n".format(result[country].get("subs", 0)))

这也是通用的,因为你可以添加额外的数字列(例如janitors:D),它会愉快地将它们加在一起,但请记住它只适用于整数(如果你想要浮点数,请替换使用float引用int并且它期望除了school之外的每个字段都是一个数字。

答案 1 :(得分:0)

您的问题可以通过以下方式解决:

import csv

search = raw_input('Enter school (e.g. us: ')
with open('data.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    result_countrys = {}
    for row in reader:
      students = int(row['students'])
      teachers = int(row['teachers'])
      subs = int(row['subs'])
      subs = row['subs']
      country = school[: 2]
      if country in result_countrys:
        count = result_countrys[country]
        count['students'] = count['students'] + students
        count['teachers'] = count['teachers'] + teachers
        count['subs'] = count['subs'] + subs
      else :
        dic = {}
        dic['students'] = students
        dic['teachers'] = teachers
        dic['subs'] = subs
        result_countrys[country] = dic

for k, v in result_countrys[search].iteritems():
    print("country " + str(search) + " has " + str(v) + " " + str(k))

我尝试了这组价值观:

reader = [{'school': 'us-school1', 'students': 20, 'teachers': 6, 'subs': 2}, {'school': 'us-school2', 'students': 20, 'teachers': 6, 'subs': 2}, {'school': 'uk-school1', 'students': 20, 'teachers': 6, 'subs': 2}]

结果是:

Enter school (e.g. us):  us
country us has 30 students
country us has 6 teachers
country us has 2 subs