通过循环从CSV获取数据数组

时间:2016-01-19 11:19:03

标签: python arrays parsing csv

我的CSV看起来像这样:

0.500187550,CPU1,7.93
0.500187550,CPU2,1.62
0.500187550,CPU3,7.93
0.500187550,CPU4,1.62
1.000445359,CPU1,9.96
1.000445359,CPU2,1.61
1.000445359,CPU3,9.96
1.000445359,CPU4,1.61
1.500674877,CPU1,9.94
1.500674877,CPU2,1.61
1.500674877,CPU3,9.94
1.500674877,CPU4,1.61

第一列是时间,第二列是CPU,第三列是能量。

作为最终结果,我想拥有这些数组:

时间:

[0.500187550, 1.000445359, 1.500674877]

能量(每CPU):例如CPU1

[7.93, 9.96, 9.94]

用于解析我正在使用的CSV:

query = csv.reader(csvfile, delimiter=',', skipinitialspace=True)
#Arrays global time and power:
for row in query:
    x = row[0]
    x = float(x)
    x_array.append(x) #column 0 to array
    y = row[2]
    y = float(y)
    y_array.append(y) #column 2 to array
print x_array
print y_array

通过这些方式,我可以将时间和精力的所有数据分成两个数组:x_arrayy_array

然后我订购数组:

energy_core_ord_array = []
time_ord_array = []
#Dividing array into energy and time per core:
for i in range(number_cores[0]):
    e =  0 + i
    for j in range(len(x_array)/(int(number_cores[0]))):
        time_ord = x_array[e]
        time_ord_array.append(time_ord)
        energy_core_ord = y_array[e]
        energy_core_ord_array.append(energy_core_ord)
        e = e + int(number_cores[0])

最后,我把时间阵列切成了它应该具有的长度:

final_time_ord_array = []
for i in range(len(x_array)/(int(number_cores[0]))):
    final_time_ord = time_ord_array[i]
    final_time_ord_array.append(final_time_ord)

直到这里,尽管代码不优雅,但它仍然有效。 当我尝试为每个核心获取阵列时,问题出现了。

我得到它的第一个核心,但是当我尝试迭代下一个核心时,我不知道该怎么做,我怎么能将每个数组存储在一个带有单个名称的变量中。 / p>

final_energy_core_ord_array = []
#Trunk energy core array:
for i in range(len(x_array)/(int(number_cores[0]))):
    final_energy_core_ord = energy_core_ord_array[i]
    final_energy_core_ord_array.append(final_energy_core_ord)

2 个答案:

答案 0 :(得分:3)

因此,使用Pandas(用于处理Python中数据帧的库),您可以执行类似这样的操作,这比尝试手动处理CSV要快得多:

import pandas as pd

csvfile = "C:/Users/Simon/Desktop/test.csv"

data = pd.read_csv(csvfile, header=None, names=['time','cpu','energy'])

times = list(pd.unique(data.time.ravel()))

print times

cpuList = data.groupby(['cpu'])

cpuEnergy = {}

for i in range(len(cpuList)):
    curCPU = 'CPU' + str(i+1)
    cpuEnergy[curCPU] = list(cpuList.get_group('CPU' + str(i+1))['energy'])

for k, v in cpuEnergy.items():
    print k, v

将输出以下内容:

[0.50018755000000004, 1.000445359, 1.5006748769999998]
CPU4 [1.6200000000000001, 1.6100000000000001, 1.6100000000000001]
CPU2 [1.6200000000000001, 1.6100000000000001, 1.6100000000000001]
CPU3 [7.9299999999999997, 9.9600000000000009, 9.9399999999999995]
CPU1 [7.9299999999999997, 9.9600000000000009, 9.9399999999999995]

答案 1 :(得分:0)

最后我得到了答案,使用全局变量....不是一个好主意,但是有效,如果有人发现它有用,请留在这里。

    final_energy_core_ord_array = []
    #Trunk energy core array:
    a = 0
    for j in range(number_cores[0]):
        for i in range(len(x_array)/(int(number_cores[0]))):
            final_energy_core_ord = energy_core_ord_array[a + i]
            final_energy_core_ord_array.append(final_energy_core_ord)
        globals()['core%s' % j] = final_energy_core_ord_array
        final_energy_core_ord_array = []
        a = a + 12

    print 'Final time and cores:'
    print final_time_ord_array
    for j in range(number_cores[0]):
        print globals()['core%s' % j]