简体   繁体   中英

How do I get specific columns with the same name from a csv file in Python

I hope you can help me solve this problem guys.

This is the csv file (it goes down to over 4200 columns)

图片

I need to get ALL the "electromotor_rpm"s with the associated values and units and then save it to a list. Afterwards I want to create a new csv file to analyze it and get the max, min and median from that data.

The file is very large, so there could be 200 "electromotor_rpm" columns.

I tried different approaches but couldn't find anything that could help me finally.

I found a solution:

import csv
import statistics
from csv import DictReader

el_list = []
electromotor_rpm_list = []
electromotor_rpm_stat = []
f = open('MAN.csv', "r", newline='')

werte_csv = csv.DictReader(f, delimiter=';')

for line in werte_csv:
    ausgabe = f"{line['name']}: {line['value']} {line['unit']}"

    if (line['name'] == 'electromotor_rpm'):
        electromotor_rpm_list.append(float(line['value']))


print(electromotor_rpm_list)
electromotor_rpm_stat.append(statistics.mean(electromotor_rpm_list))
electromotor_rpm_stat.append(max(electromotor_rpm_list))
electromotor_rpm_stat.append(min(electromotor_rpm_list))
electromotor_rpm_stat.append(max(electromotor_rpm_list)- 
min(electromotor_rpm_list))
print(statistics.mean(electromotor_rpm_list))
print(electromotor_rpm_stat)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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