简体   繁体   English

将不同字典中相同键的多个值相加 Python

[英]Sum Multiple Values for Same Key in Different Dictionaries Python

I have a filepath full of CSV files.我有一个充满 CSV 文件的文件路径。 I am using Python glob to open them and csv.DictReader() to read through them and parse the data into dictionaries with the headers as keys.我正在使用 Python glob打开它们,并使用csv.DictReader()读取它们并将数据解析为以标题为键的字典。

The data in the CSV files looks like this: CSV 文件中的数据如下所示:

CSVfile1: CSV 文件 1:

Name,A,B,C,D,Date
John,-1,2,4.0,-5.1,3/23/2016
Jacob,0,3,2.0,-2.3,3/23/2016
Jinglehimmer,1,100,5.0,-.1,3/23/2016

CSVfile2: CSV文件2:

Name,A,B,C,D,Date
John,5,4,1.0,-1,3/24/2016
Jacob,0,1,7.0,-.1,3/24/2016
Schmidt,10,9,8,7,3/24/2016

I am trying to SUM the data in the A , B , C and D columns for each name over a set date period (say the past 2 days).我正在尝试将ABCD列中每个名称在设定日期期间(例如过去 2 天)的数据求和。 For example, I am trying to get a new list of dictionaries that looks like this:例如,我正在尝试获取一个新的字典列表,如下所示:

{Name: John, A: 4, B: 6, C: 5.0, D: -6.1, Date: 2}
{Name: Jacob, A: 0, B: 4, C: 9.0, D: -2.4, Date: 2}
{Name: Jinglehimmer, etc.}
{Name: Schmidt, etc.}

Here is the code I have so far that I know works.这是我目前知道的有效代码。 This opens each CSV and creates a dictionary for each row and allows me to iterate through the dictionaries:这将打开每个 CSV 并为每一行创建一个字典,并允许我遍历字典:

import csv
import glob

path = "."

newdict = {}

for filename in glob.glob(path):
    with open(filename) as csv_file:
        for row in csv.DictReader(csv_file):

Edit: I tried simply summing all the key values into a new dictionary, but I run into an int+str error.编辑:我尝试简单地将所有键值汇总到一个新字典中,但遇到了 int+str 错误。

for k in row.keys():
    newdict[k] = newdict.get(k,0) + row[k]

I am also not sure how to filter by the Date: key to only get x days of data.我也不确定如何按Date:键过滤以仅获取 x 天的数据。

Any help or points in the right direction are much appreciated.任何帮助或正确方向的观点都非常感谢。

The following approach should work:以下方法应该有效:

import csv
import glob
from datetime import datetime, timedelta, date


days = 2
since = datetime.combine(date.today(), datetime.min.time()) - timedelta(days = days)
required_fields = ['A', 'B', 'C', 'D']

path = "."
newdict = {}

output = {}

for filename in glob.glob(path):
    with open(filename) as csv_file:
        for row in csv.DictReader(csv_file):
            if datetime.strptime(row['Date'], '%m/%d/%Y') >= since:
                name = row['Name']

                try:
                    cur_entry = output[name]
                    entry = {field : cur_entry[field] + float(row[field]) for field in required_fields}
                except KeyError as e:
                    entry = {field : float(row[field]) for field in required_fields}
                    entry['Date'] = days

                output[name] = entry

for name, entry in output.items():                
    print name, entry

Which for the data you have given will display:您提供的数据将显示:

Jacob {'A': 0.0, 'C': 9.0, 'B': 4.0, 'D': -2.4}
Jinglehimmer {'A': 1.0, 'Date': 2, 'C': 5.0, 'B': 100.0, 'D': -0.1}
John {'A': 4.0, 'C': 5.0, 'B': 6.0, 'D': -6.1}
Schmidt {'A': 10.0, 'Date': 2, 'C': 8.0, 'B': 9.0, 'D': 7.0}

A datetime object can be used to help with measuring the time interval. datetime对象可用于帮助测量时间间隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM