2017-10-04 142 views
0

多值文件我有一個CSV結構如下文件:閱讀CSV每個標籤

# Parameters :,   P1, P2, P3 
2012-01-01 01:01:55.000000,1,2,3,4,5,6,6,8,9 
2012-01-01 01:01:56.000000,4,9,2,0,2,1,1,6,8 
... 

如何使用Python的(和可選熊貓)有以下結果來閱讀:

{'2012-01-01 01:01:55.000000': {'P1': [1, 2, 3], 'P2': [4, 5, 6], 'P3': [7, 8, 9]}, 
'2012-01-01 01:01:56.000000': {'P1': [4, 9, 2], 'P2': [0, 2, 1], 'P3': [1, 6, 8]}} 

謝謝!

+2

您可以添加更多行,並解釋你的輸出應看起來像?一行不是很具描述性。 –

+0

1,2,3和4,5,6之間的區別是什麼?他們是如何分配給P1或P2的邏輯? – brddawg

+0

@coldspeed:完成 – Nicolargo

回答

0

隨着熊貓和numpy的

with open('tst.csv') as f: 
    _, *params = map(str.strip, f.readline().split(',')) 

d1 = pd.read_csv(
    'tst.csv', comment='#', header=None, 
    index_col=0, parse_dates=True) 

i = d1.index.rename(None) 
v = d1.values 
t = v.reshape(v.shape[0], -1, v.shape[1] // len(params)).transpose(1, 0, 2) 

pd.DataFrame(dict(zip(params, t.tolist())), i) 

          P1   P2   P3 
2012-01-01 01:01:55 [1, 2, 3] [4, 5, 6] [6, 8, 9] 
2012-01-01 01:01:56 [4, 9, 2] [0, 2, 1] [1, 6, 8] 

沒有

with open('tst.csv') as f: 
    _, *params = map(str.strip, f.readline().split(',')) 
    k = len(params) 
    d = {ts: dict(zip(
     params, 
     (data[i*len(data)//k:(i+1)*len(data)//k] for i in range(k)) 
    )) for ts, *data in map(lambda x: x.strip().split(','), f.readlines())} 

d 

{'2012-01-01 01:01:55.000000': {'P1': ['1', '2', '3'], 
           'P2': ['4', '5', '6'], 
           'P3': ['6', '8', '9']}, 
'2012-01-01 01:01:56.000000': {'P1': ['4', '9', '2'], 
           'P2': ['0', '2', '1'], 
           'P3': ['1', '6', '8']}} 
1

csv.reader隨着對象和itertools.islice()功能:

import csv, itertools 

with open('test.csv', 'r') as f: 
    reader = csv.reader(f, delimiter=',', skipinitialspace=True) 
    header = next(reader)[1:] # getting `P<number>` keys 
    d = {} 
    for l in reader: 
     d[l[0]] = {header[i]: list(itertools.islice(l[1:], i*3, i*3+3)) for i in range(len(header))} 

print(d) 

的輸出(3個輸入行):

{'2012-01-01 01:01:55.000000': {'P2': ['4', '5', '6'], 'P1': ['1', '2', '3'], 'P3': ['6', '8', '9']}, '2012-01-01 01:01:56.000000': {'P2': ['0', '2', '1'], 'P1': ['4', '9', '2'], 'P3': ['1', '6', '8']}} 

。注意,字典在Python是無序的結構。
爲了得到一個有序的結構定義結果字典爲OrderedDict對象(從collections模塊)

... 
d = collections.OrderedDict() 

在這種情況下,結果將是:

OrderedDict([('2012-01-01 01:01:55.000000', {'P1': ['1', '2', '3'], 'P2': ['4', '5', '6'], 'P3': ['6', '8', '9']}), ('2012-01-01 01:01:56.000000', {'P1': ['4', '9', '2'], 'P2': ['0', '2', '1'], 'P3': ['1', '6', '8']})])