Python 3.x Python3-从&;将值写入.csv文件

Python 3.x Python3-从&;将值写入.csv文件,python-3.x,average,export-to-csv,Python 3.x,Average,Export To Csv,我正在读取一个.csv文件,想从中提取某些值并将其写入一个新的result.csv(B)文件。我试着用代码(A)来做这件事,它只是部分工作 在定义中,我将所有变量放入其中,最终从我正在读取的.csv文件中提取匹配值。(除了“记录id”和“缩写”,因为我将手动填写) 现在,通过运行代码(A),它将在result.csv中生成以下输出: 电流输出 record_id abbreviation patient_id step_count distance ambulation_time v

我正在读取一个.csv文件,想从中提取某些值并将其写入一个新的result.csv(B)文件。我试着用代码(A)来做这件事,它只是部分工作

在定义中,我将所有变量放入其中,最终从我正在读取的.csv文件中提取匹配值。(除了“记录id”和“缩写”,因为我将手动填写)

现在,通过运行代码(A),它将在result.csv中生成以下输出:

电流输出

record_id  abbreviation  patient_id  step_count  distance  ambulation_time  velocity  cadence  normalized_velocity  step_time_differential  step_length_differential  cycle_time_differential  step_time  step_length  step_extremity  cycle_time  stride_length  hh_base_support  swing_time  stance_time  single_supp_time  double_supp_time  toe_in_out 
70520161453                          3           292,34    1,67             ,         107,8    ,                    0,004                   1,051                     0,008                    ,          96,746       ,               1,116       ,              2,988            ,           ,            ,                 ,
record_id  abbreviation  patient_id  step_count  distance  ambulation_time  velocity  cadence  normalized_velocity  step_time_differential  step_length_differential  cycle_time_differential  step_time  step_length  step_extremity  cycle_time  stride_length  hh_base_support  swing_time  stance_time  single_supp_time  double_supp_time  toe_in_out 
70520161453              25          3           292,34    1,67             175,1     107,8                         0,004                   1,051                     0,008                    0,56       97,27                        1,11        194,64         4,65             0,47        0,65         0,47              0,18              1,45
正如您所看到的,与所需输出(B)相比,有许多值缺失,也有一些显示,但不正确

现在我面临的问题如下:

问题1

因为我正在比较我正在读取的.csv文件中的名称与我定义中的名称(A)。其中一些名称不完全匹配,或者与部分同名的其他名称混淆

这就是问题所在:

patient_id, velocity, step_time, stride_length, swing_time, stance_time, single_supp_time, double_supp_time, toe_in_out
例如,我的定义中的velocity与我正在读取的.csv文件中的velocity匹配,但它也与StradVelocitySDdev匹配。这会导致速度值丢失

问题2

以下所有变量都包含2个值而不是1,例如步骤时间包含值0558和0554。对于所有这些包含2个值的变量,我想计算两个值的平均值,然后只将平均值(在本例中,0558&0554的平均值=0,56)写入属于步骤时间的result.csv

step_time, step_length, cycle_time, stride_length, hh_base_support, swing_time, stance_time, single_supp_time, double_supp_time, toe_in_out
希望有人能帮我解决这些问题,将不胜感激

请随意使用我正在使用的导出文件,您可以在此处下载:

(A)Python代码

import csv
from collections import defaultdict
from datetime import datetime

data = defaultdict(str)
result = 'path/to/file/result_%s.csv'%datetime.now().strftime('%b-%d-%Y_%H%M')

#Make a list with the predefined variables
definition = ["record_id", "abbreviation", "patient_id", "step_count", "distance", "ambulation_time", "velocity", "cadence", "normalized_velocity", "step_time_differential", "step_length_differential", "cycle_time_differential", "step_time", "step_length", "step_extremity", "cycle_time", "stride_length", "hh_base_support", "swing_time", "stance_time", "single_supp_time", "double_supp_time", "toe_in_out"]

#Read the GaitRite .csv
with open('path/to/file/Export 4.csv', 'r') as f, open(result, 'w') as outfile:
    reader = csv.reader(f, delimiter=';')
    next(reader, None)  # skip the headers
    writer = csv.DictWriter(outfile, fieldnames=definition, lineterminator='\n')
    writer.writeheader()

#Read the .csv row by row
    for row in reader:
        for item in definition:
            h = item.replace('_', '')
            r0 = row[0].lower().replace(' ', '')
            if h in r0:
                try:
                    avg = round((float(row[1].replace(',', '.')) + float(row[2].replace(',', '.'))) / 2, 2)
                    data[item] = avg
                except ValueError:
                    avg = 0  # for cases with entry strings or commas
                data[item] = row[1]

    data['record_id'] = datetime.now().strftime('%m%d%Y%H%M')

# Write the clean result.csv
    writer.writerow(data)
(B)所需的.csv输出

record_id  abbreviation  patient_id  step_count  distance  ambulation_time  velocity  cadence  normalized_velocity  step_time_differential  step_length_differential  cycle_time_differential  step_time  step_length  step_extremity  cycle_time  stride_length  hh_base_support  swing_time  stance_time  single_supp_time  double_supp_time  toe_in_out 
70520161453                          3           292,34    1,67             ,         107,8    ,                    0,004                   1,051                     0,008                    ,          96,746       ,               1,116       ,              2,988            ,           ,            ,                 ,
record_id  abbreviation  patient_id  step_count  distance  ambulation_time  velocity  cadence  normalized_velocity  step_time_differential  step_length_differential  cycle_time_differential  step_time  step_length  step_extremity  cycle_time  stride_length  hh_base_support  swing_time  stance_time  single_supp_time  double_supp_time  toe_in_out 
70520161453              25          3           292,34    1,67             175,1     107,8                         0,004                   1,051                     0,008                    0,56       97,27                        1,11        194,64         4,65             0,47        0,65         0,47              0,18              1,45