2013-02-26 34 views
1

如果我的問題在下面丟失,我需要向我的家庭自動化系統呈現的是一個數組,我可以從單元格中檢索信息。Python將字符串轉換爲固定列數組

我使用下面的代碼從民調我家HVAC系統(大部分是從別人的帖子複製很差這裏)串行設備閱讀:

import time 
import serial 
import StringIO 

# configure the serial connections 
ser = serial.Serial(
     port='/dev/ttyS0', 
     baudrate=9600, 
     parity=serial.PARITY_NONE, 
     stopbits=serial.STOPBITS_ONE, 
     bytesize=serial.EIGHTBITS 
) 

input=1 
while 1 : 
     # Use static command to debug 
     input = "stats" 
     # Python 3 users 
     # input = input(">> ") 
     if input == 'exit': 
       ser.close() 
       exit() 
     else: 
       # send the character to the device 
       # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device) 
       ser.write(input + '\r\n') 
       outputFCUData = '' 
       # let's wait one second before reading output (let's give device time to answer) 
       time.sleep(1) 
       while ser.inWaiting() > 0: 
         outputFCUData += ser.read(1) 

       if outputFCUData != '': 
         fcuArrayTemp = StringIO.StringIO(outputFCUData).read().splitlines() 
         fcuArrayTemp.pop(0) 
         fcuArrayTemp.pop(-1) 
         fcuArrayTemp.pop(-1) 
         fcuArrayTemp.pop(-1) 
         print fcuArrayTemp 
         exit() 

如果我是輪詢設備不帶任何格式,其結果必然是:

stats 
101 ON 070F 070F Low Heat OK 0 
102 ON 069F 069F Low Heat OK 0 
103 ON 069F 069F Low Heat OK 0 
104 ON 069F 070F Low Heat OK 0 
105 OFF 072F 064F High Heat U5 0 
OK 
> 
> 

這是當我pop(0)pop(-1)的代碼刪除所有,但5行的信息,我想要的。對於任何好奇的人來說,第一列(例如「101」)是我的fancoil名稱,後面跟着狀態,設定值,當前溫度,風扇速度,模式(加熱/冷卻),錯誤代碼(例如105沒有t -stat,所以它有一個U5 error),然後最後一列是發送命令到設備的錯誤 - 現在沒有,因此是「0」。

所以我想把這個輸出結果轉換成一個數組,這樣我就可以調用fcuStatus[i][j]命令將信息從單元格(i,j)中提取出來。

我從我的代碼得到的是以下幾點:

['101 ON 070F 070F Low Heat OK 0', '102 ON 069F 069F Low Heat OK 0', '103 ON 069F 069F Low Heat OK 0', '104 ON 069F 070F Low Heat OK 0', '105 OFF 072F 064F High Heat U5 0'] 

這是一個1行5列的列表。我相信我應該只需要讀取列表中的元素並將它們添加到數組中。所以我添加代碼:

for element in fcuArrayTemp 
    parts = element.split(' ') 
    print parts 

所以現在我的輸出是:

['101', 'ON', '', '070F', '070F', '', 'Low', '', 'Heat', 'OK', '0'] 
['102', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0'] 
['103', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0'] 
['104', 'ON', '', '069F', '069F', '', 'Low', '', 'Heat', 'OK', '0'] 
['105', 'OFF', '072F', '064F', '', 'High', '', 'Heat', 'U5', '0'] 

這是非常接近我想要的東西,但也有添加爲我分割上的結果,一些額外的列有雙空白時單空白。

我的代碼是馬虎,我不得不相信有更好的方法。有人能告訴我如何獲取我在我的outputFCUData變量中收到的字符串信息,並將其轉換爲沒有多餘空格的函數數組?總是會有8列,但是隨着fancoils被添加到系統中,陣列可能會擴展到128+行。以上任何一種情況都是因爲我不知道更好,並不是因爲我試圖符合一套特定的指導方針 - 任何建議都非常值得歡迎。

EditWow-無線電 - 讓我正是我需要的 - 謝謝你!

for element in fcuArrayTemp 
    parts = element.split() 
    print parts 

所以,真正的這最後一部分是我怎麼然後把這些組織的名單,並通過8列矩陣創建A N行?這個錯誤沒有給出附加的參數。將「元素」添加到要附加的項目(fcuArray.append(element))也不會讓我在那裏。

fcuArray = [] 
for element in parts: 
    fcuArray = fcuArray.append() 
    print fcuArray 

再次感謝

編輯:找到一個能用我 - 在這裏張貼的任何其他人正在尋找類似的解決方案。訣竅是從列表中的每一行添加到我的陣列,因爲他們產生的:

fcuArray = [] 
for element in fcuArrayTemp 
    parts = element.split() 
    fcuArray.append(parts) 

現在我可以通過請求行和位置報告陣列中的任何值。例如,要報告數組中的第3個風扇盤管的名稱,我會要求fcuArray [3] [0](即「print fcuArray [3] [0]」,它將返回「104」。我的完整代碼:

import time 
import serial 
import StringIO 
import pprint 

# configure the serial connections 
ser = serial.Serial(
     port='/dev/ttyS0', 
     baudrate=9600, 
     parity=serial.PARITY_NONE, 
     stopbits=serial.STOPBITS_ONE, 
     bytesize=serial.EIGHTBITS 
) 

input=1 
while 1 : 
     # Use static command to debug 
     input = "stats" 
     # Python 3 users 
     # input = input(">> ") 
     if input == 'exit': 
       ser.close() 
       exit() 
     else: 
       # send the character to the device 
       # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device) 
       ser.write(input + '\r\n') 
       outputFCUData = '' 
       # let's wait one second before reading output (let's give device time to answer) 
       time.sleep(1) 
       while ser.inWaiting() > 0: 
         outputFCUData += ser.read(1) 

       if outputFCUData != '': 
         fcuArrayTemp = StringIO.StringIO(outputFCUData).read().splitlines() 
         fcuArrayTemp.pop(0) 
         fcuArrayTemp.pop(-1) 
         fcuArrayTemp.pop(-1) 
         fcuArrayTemp.pop(-1) 
       fcuArray = [] 
       for element in fcuArrayTemp: 
          parts = element.split() 
       fcuArray.append(parts) 
       print fcuArray 
       print fcuArray[3][0] 
       exit() 

回答

1

更改element.split(' ')element.split()就足以去除多餘的欄

>>> for element in fcuArrayTemp: 
...  print element.split() 
... 
['101', 'ON', '070F', '070F', 'Low', 'Heat', 'OK', '0'] 
['102', 'ON', '069F', '069F', 'Low', 'Heat', 'OK', '0'] 
['103', 'ON', '069F', '069F', 'Low', 'Heat', 'OK', '0'] 
['104', 'ON', '069F', '070F', 'Low', 'Heat', 'OK', '0'] 
['105', 'OFF', '072F', '064F', 'High', 'Heat', 'U5', '0']