python读取.csv文件中的数据

python 读取.csv文件中数据的库有两个,一个是csv库,一个是pandas库。

调用读取指令读取数据后得到一个csv对象,想要用numpy等库处理时候,需要对其进行转型。

csv读取

import os
import csv
import matplotlib.pyplot as plt
import numpy as np

data_sourse_dir = "G:\\project\\"

# 遍历数据结果下的所有文件
testids = os.listdir(data_sourse_dir)
fig = plt.figure(1)
legend_name1 = list()
for testid in testids:
    if "target_name1" in testid:
        real_data_dir = data_sourse_dir + testid + "\\"
        testids2 = os.listdir(real_data_dir)
        for testid2 in testids2:
            if "agcBigScaleStatisticTable" in testid2:
                ful_data_dir = real_data_dir + testid2
                data_file = open(ful_data_dir,'r')
                data = csv.reader(data_file)
                counter_max_value = [row[20] for row in data]
                counter_max_value = list(map(int,counter_max_value))
                plt.plot(counter_max_value)
                legend_name1.append(testid)
plt.legend(legend_name1,loc = "upper right")

fig = plt.figure(2)
legend_name2 = []
for testid in testids:
    if "target_name2" in testid:
        real_data_dir = data_sourse_dir + testid + "\\"
        testids2 = os.listdir(real_data_dir)
        for testid2 in testids2:
            if "agcBigScaleStatisticTable" in testid2:
                ful_data_dir = real_data_dir + testid2
                data_file = open(ful_data_dir,'r')
                data = csv.reader(data_file)
                counter_max_value = [row[20] for row in data]
                counter_max_value = list(map(int,counter_max_value))
                plt.plot(counter_max_value)
                legend_name2.append(testid)
plt.legend(legend_name2,loc = "upper right")

fig = plt.figure(3)
legend_name3 = []
for testid in testids:
    if "target_name3" in testid:
        real_data_dir = data_sourse_dir + testid + "\\"
        testids2 = os.listdir(real_data_dir)
        for testid2 in testids2:
            if "agcBigScaleStatisticTable" in testid2:
                ful_data_dir = real_data_dir + testid2
                data_file = open(ful_data_dir,'r')
                data = csv.reader(data_file)
                counter_max_value = [row[20] for row in data]
                counter_max_value = list(map(int,counter_max_value))
                plt.plot(counter_max_value)
                legend_name3.append(testid)
plt.legend(legend_name3,loc = "upper right")

plt.show()

pandas 读取

import pandas as pd
from matplotlib import pyplot as plt

agc_param = pd.read_csv(unit_test_output_dir + test_csv, index_col=None, dtype={'voiceProb': 'float64', 'energyProb': 'float64', 'snr': 'float64', 'firstStageGain': 'float64'})
agc_param.to_numpy()

key = 'target_key_to_be_ploted'
plt.plot(agc_param[key])

plt.show()

猜你喜欢

转载自blog.csdn.net/ljl86400/article/details/109720720