2017-06-04 46 views
0

我曾嘗試使用Python CSV閱讀txt文件,然後在圖中使用python matplotlib在文本文件中的數據閱讀文本文件和現在的數據在Python matplotlib

簡單介紹:

261 P 0.18 0 834 64627 0 768 0 320 834 64627 0 768 0 320 (radio 1.17%/1.17% tx 0.00%/0.00% listen 1.17%/1.17%) 

這用於閱讀文件的python腳本,但它不起作用並且沒有錯誤消息。

import csv 
import matplotlib.pyplot as plt 

# for P lines 
#0-> str, 
#1 -> clock_time(),2-> P, 3->rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], 4-> seqno, 
#5 -> all_cpu,6-> all_lpm,7-> all_transmit,8-> all_listen,9-> all_idle_transmit,10-> all_idle_listen, 
#11->cpu,12-> lpm,13-> transmit,14-> listen, 15 ->idle_transmit, 16 -> idle_listen, [RADIO STATISTICS...] 


from collections import defaultdict 
cpuOverTime = defaultdict(list) 

with open('loglistener.txt', 'rb') as f: 
    reader = csv.reader(f,delimiter=' ') 
    for row in reader: 
     if row[2] is 'P': 
      cpuOverTime[row[3]].append(row[11]) 

for i in cpuOverTime: 
    plt.plot(cpuOverTime[i]) 
plt.show() 
########## 
+0

你的意思是'如果行[1]是'P':'?偏移量從0開始,所以第二個項目在偏移量1處。 – FujiApple

回答

0

在Python,如同大多數編程語言常見,偏移量從0開始,而不是從1

從您的樣本數據,我們可以看出,P是排在第二空間分隔的項目,因此具有1索引:

Offset 0 | 1 | 2 | 3 | 4 | 5  | 6 
Row  261 | P | 0.18 | 0 | 834 | 64627 | 0 

要解決該問題簡單地改變索引爲1:

with open('loglistener.txt', 'rb') as f: 
    reader = csv.reader(f,delimiter=' ') 
    for row in reader: 
     if row[1] is 'P': 
      cpuOverTime[row[3]].append(row[11]) 

我還沒有檢查,但你可能有相同的問題row[3]row[11]

+0

現在運行良好,在PI應該包括00之前有兩個值:00:02.396 \t ID:8 \t 261 P 0.18 0 834 64627 0 768 0 320 834 64627 0 768 0 320(收音機1.17%/ 1.17%tx 0.00%/ 0.00%聽1.17%/ 1.17%) –