Python 3 - 计算平均值&写信给.csv

时间:2016-06-30 09:05:33

标签: python csv average

(A)Python代码

import csv
from collections import defaultdict

data = defaultdict(str)

#Make a list with the predefined variables
definition = ["record_id", "abbreviation", "patient_id", "study_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('C:/Users/Kay_v/Documents/School/Exports/Export 3.csv', 'r')  as f, open('C:/Users/Kay_v/Documents/School/Exports/result.csv', '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:
        #print(row)
        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)
            except ValueError:
                avg = 0  # for cases with entry strings or commas
                #print(avg)
                print(h, r0, row[1], row[2])
                data[item] = row[1]

    data['record_id'] = 1

# Write the clean result.csv
    writer.writerow(data)

(B)问题

问题在于将平均值包含在result.csv中。 每当变量有两个值时,我使用以下部分代码来计算平均值。在当前情况下,计算平均值,但它没有显示在result.csv

try:
    avg = round((float(row[1].replace(',', '.')) + float(row[2].replace(',', '.'))) / 2, 2)
except ValueError:
    avg = 0  # for cases with entry strings or commas

我希望任何人都能帮助平均值出现在result.csv中,我们将非常感谢!

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

3 个答案:

答案 0 :(得分:0)

试试这个:

if h in r0:
    try:
        avg = round((float(row[1].replace(',', '.').replace(';', '.')) + float(row[2].replace(',', '.').replace(';', '.'))) / 2, 2)
        data[item] = avg
    except ValueError:
        data[item] = 0  # for cases with entry strings or commas
        #print(avg)
        print(h, r0, row[1], row[2])

答案 1 :(得分:0)

您正在计算平均值但未将其添加到csv文件中,这是您面临的第一个问题 - 我从您的模糊问题中理解的内容。 首先在列表definition中添加另一列,然后将名为average的密钥添加到dict data中,这是修改后的代码:

import csv
from collections import defaultdict

data = defaultdict(str)

#Make a list with the predefined variables
definition = ["record_id", "abbreviation", "patient_id", "study_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", "average"]

#Read the GaitRite .csv
with open('Export 3.csv', 'r')  as f, open('result.csv', '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:
        #print(row)
        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)
                except ValueError:
                    avg = 0  # for cases with entry strings or commas
                # print(avg)
                # print(h, r0, row[1], row[2])
                data[item] = row[1]
                data['average'] = avg    
    data['record_id'] = 1

# Write the clean result.csv
    print data
    writer.writerow(data)

答案 2 :(得分:0)

我会尝试以更好的方式解释它。我希望result.csv最终输出以下内容:

所需的输出

set fp [open "test.tcl" r]
set new_file [open "final.tcl" w+]
while {![eof $fp]} {
     get $fp line
     if $line {$'{'} {
     puts $new_file '\n'
     } 
}

问题是我正在读取数据的.csv中的一些值包含2个值(如step_time [0,558; 0,554])而其他值只包含1个值(如step_count [3])。只包含1个值的那些可以立即传递给result.csv。但是对于包含2个值的那些,应该计算这两个值的平均值,然后该值也应该传递给result.csv