2015-12-21 53 views
1

我正在使用arduino和python創建RFID卡數據收集的代碼。我已經制定了如何將arduino串行輸出提供給Python,並將其保存到名爲Card ID的文件中,這非常棒。將文本數據轉換爲可以在python中清理的數組?

現在我需要清理數據,但是我無法繞開它。 此處的數據(不知道如何意圖在這裏,FML):

Card UID: 0A 2E 45 35 
PICC type: MIFARE 1KB 
Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits 
    15  63 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     62 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    14  59 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     58 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     57 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     56 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    13  55 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     53 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     52 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    12  51 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    11  47 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     46 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     45 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     44 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    10  43 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     42 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    9  39 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     38 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     37 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    8  35 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    7  31 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     29 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     28 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    6  27 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     26 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     25 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     24 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    5  23 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     21 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    4  19 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     17 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    3  15 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     14 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
     12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    2  11 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
     10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
      9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
      8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
    1  7 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
      6 09 05 00 15 33 14 00 00 00 00 00 00 00 00 00 00 [ 0 0 0 ] 
      5 73 73 73 73 73 73 73 73 73 73 73 73 73 73 42 95 [ 0 0 0 ] 
      4 20 20 5A 76 69 65 64 72 69 73 73 73 73 73 73 73 [ 0 0 0 ] 
    0  3 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF [ 0 0 1 ] 
      2 73 73 73 73 73 73 73 73 73 73 73 73 73 73 42 95 [ 0 0 0 ] 
      1 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 [ 0 0 0 ] 
      0 0A 2E 45 35 54 88 04 00 85 00 B4 2E F0 BB 6A A8 [ 0 0 0 ] 

我怎樣才能將其轉換爲在anarray形狀,其中列和行可以與製表刪除等間隔的文本?我只需要HEX數據,現在它被解釋爲\ n的字符串。

+0

我會使用每行的正則表達式。 – tglaria

回答

1

我不完全確定你需要什麼,但好像你想要一個二維列表,其中包含十六進制數據與列和行的順序。

如果確實是這樣,我會做這樣的:一步

data = """73 73 73 73 73 73 73 73 73 73 73 73 73 73 42 95 [ 0 0 0 ] 
73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 [ 0 0 0 ] 
0A 2E 45 35 54 88 04 00 85 00 B4 2E F0 BB 6A A8 [ 0 0 0 ]""" 

lst = map(lambda x: x[:-11].replace(" "," ").split(" "),data.split("\n")) 
print lst 

說明步:

數據被編組爲多行字符串。

map是一個函數,它允許您在列表中處理列表中相對於給定函數(map()中的左參數)的列表中的所有元素。

喜歡:

lst = ["1", "2", "3", "4", "5"] 
map(int, lst) 

會給你一個整數列表,而不是字符串列表。 和等價於:

map(lambda x: int(x), lst) 

正如你看到所有我們做的是定義一個函數列表中的改變每個項目。

這是我在示例中做的,我通過用[:-11]剪輯結尾來更改每一行。這就是爲什麼我使用地圖功能,當然你可以用for循環來輕鬆完成。如果您需要解決方案中關於split()的解釋,請提及。

它給你;

[['73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '42', '95'], 
['73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73', '73'], 
['0A', '2E', '45', '35', '54', '88', '04', '00', '85', '00', 'B4', '2E', 'F0', 'BB', '6A', 'A8']] 

現在您可以通過列和行操作訪問數據並對其進行更改。

如果不是你所需要的,這可能是因爲你沒有詳細說明你需要什麼,如果你能給出詳細的細節我當然可以幫助更多。

+0

感謝您的回覆,我會盡力處理這個問題,但可能不是我需要的。數據混雜在額外的信息,我需要擺脫。行和列編號對我沒有任何好處,我需要的只是HEX的東西,對我來說清理它非常複雜。據我所知,這隻查找一塊日期,並將其轉換爲數組,但我不太確定它是如何工作的。你能解釋一下嗎? 「map(lambda x:x [: - 11]」對我來說太複雜了。 –

+0

如果垃圾數據位於十六進制行的左側和右側,請考慮使用[:5],[:10]類的符號,我已經在答案的最後使用了該符號,但沒有包含該行的開頭,這是枚舉行。我將在一秒內解釋 – Rockybilly

+0

我想我明白了:lst = map(lambda q:q [14:-13] .replace(「」,「」).split(「」),x.split(「\ n「))給了我想要的結果。現在只需要刪除數組中的頂部和底部線。任何偉大的方式來做到這一點? 非常感謝! –

相關問題