Python 3 - Read from & Write values to a .csv

假如想象 提交于 2019-12-02 03:25:49

问题


I am reading a .csv file, want to extract certain values from it and write these to a new result.csv (B) file. I tried doing this with the code (A), which is only partially working.

In the definition i put all the variables from which i want to eventually extract the matching values out of the .csv file i am reading. (except for "record_id" and "abbreviation", because i will fill these manually)

Now by running code (A), it generates the following output in the result.csv:

Current output

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            ,           ,            ,                 ,

As you can see there are a lot of values missing compared to the desired output (B) as well as some that are shown, but incorrect.

Now the problems i am facing are as follows:

Problem 1

Since i am comparing names in the .csv file i am reading, with the names in my definition (A). Some of them do not exactly match, or get confused with others that partially have the same name.

This is the problem for:

patient_id, velocity, step_time, stride_length, swing_time, stance_time, single_supp_time, double_supp_time, toe_in_out

For example velocity from my definition matches with velocity from the .csv file i am reading, but it also matches with stridevelocitystddev. This is causing the missing value for velocity.

Problem 2

All the following variables contain 2 values instead of 1, for example step_time contains the value 0,558 & 0,554. For all of these variables containing 2 values, i want to calculate the avarage of the two and then only write the average (in this example the average of 0,558 & 0,554 = 0,56) to the result.csv belonging to step_time.

step_time, step_length, cycle_time, stride_length, hh_base_support, swing_time, stance_time, single_supp_time, double_supp_time, toe_in_out

Hopefully someone can help me fix these problems, will be appreciated!

Feel free to play with the export file i am using, you can download it here: CSV export file

(A) Python code

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) Desired .csv output

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

来源:https://stackoverflow.com/questions/38206262/python-3-read-from-write-values-to-a-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!