2015-10-06 49 views
0

我有一個xml文件,有一些數據,我正在提取並放置在一個numpy記錄數組中。我打印陣列,我看到數據位於正確的位置。我想知道如何將這些信息放在我的numpy記錄數組中並將其放置在一個表中。當我打印記錄時,我也收到了字母b,我該如何解決?如何從python的數組中獲取記錄到表中?

XML數據

<instance name="uart-0" module="uart_16550" offset="000014"/> 
<instance name="uart-1" offset="000020" module="uart_16650"/> 

代碼在Python

inst_rec=np.zeros(5,dtype=[('name','a20'),('module','a20'),('offset','a5')]) 

for node in xml_file.iter(): 
    if node.tag=="instance": 
     attribute=node.attrib.get('name') 
     inst_rec[i]= (node.attrib.get('name'),node.attrib.get('module'),node.attrib.get('offset')) 
     i=i+1 

for x in range (0,5): 
    print(inst_rec[x]) 

輸出

(b'uart-0', b'uart_16550', b'00001') 

(b'uart-1', b'uart_16650', b'00002') 
+0

「桌子」是軟的嗎?一個2-D numpy數組?一個HTML'

'元素?一個Excel電子表格? –

+2

表是什麼意思?你的意思是數據庫?如果是這樣,哪個數據庫?從那裏你可以搜索'如何插入MYDB'。 – postelrich

+0

你可能想看看'tabulate'模塊。那就是如果你字面意思是一張桌子。該模塊能夠處理字典,列表,numpy數組和其他結構化數據,然後將它們格式化爲HTML,降價,乳膠和純文本查看。 [tabulate](https://pypi.python.org/pypi/tabulate) – Ajay

回答

0

您正在使用Python3,它使用unicode字符串。它顯示b的字節字符串。 xml文件也可以是字節,例如encoding='UTF-8'

通過在打印之前將字符串傳遞到decode(),您可以擺脫b

更多關於寫入在PY3 csv文件Numpy recarray writes byte literals tags to my csv file?

在測試中,我可以通過使inst_rec陣列使用Unicode字符串('U20'

import numpy as np 
import xml.etree.ElementTree as ET 

tree = ET.parse('test.xml') 
root = tree.getroot() 

# inst_rec=np.zeros(2,dtype=[('name','a20'),('module','a20'),('offset','a5')]) 
inst_rec = np.zeros(2,dtype=[('name','U20'),('module','U20'),('offset','U5')]) 

i = 0 
for node in root.iter(): 
    if node.tag=="instance": 
     attribute=node.attrib.get('name') 
     rec = (node.attrib.get('name'),node.attrib.get('module'),node.attrib.get('offset')) 
     inst_rec[i] = rec 
     # no need to decode 
     i=i+1 

# simple print of the array 
print(inst_rec) 

# row by row print 
for x in range(inst_rec.shape[0]): 
    print(inst_rec[x]) 

# formatted row by row print 
for rec in inst_rec: 
    print('%20s,%20s, %5s'%tuple(rec)) 

# write a csv file 
np.savetxt('test.out', inst_rec, fmt=['%20s','%20s','%5s'], delimiter=',') 

產生

[('uart-0', 'uart_16550', '00001') ('uart-1', 'uart_16650', '00002')] 

('uart-0', 'uart_16550', '00001') 
('uart-1', 'uart_16650', '00002') 

      uart-0,   uart_16550, 00001 
      uart-1,   uart_16650, 00002 
簡化顯示

and

1703:~/mypy$ cat test.out 
      uart-0,   uart_16550,00001 
      uart-1,   uart_16650,00002 

爲ASCII表顯示

# formatted row by row print 
print('----------------------------------------') 
for rec in inst_rec: 
    print('| %20s | %20s | %5s |'%tuple(rec)) 
    print('---------------------------------------') 

如果你想要的東西票友你需要指定顯示工具 - HTML,RTF文本等


與加包prettyprint

import prettytable 
pp = prettytable.PrettyTable() 
pp.field_names = inst_rec.dtype.names 
for rec in inst_rec: 
    pp.add_row(rec) 
print(pp) 

產生

+--------+------------+--------+ 
| name | module | offset | 
+--------+------------+--------+ 
| uart-0 | uart_16550 | 00001 | 
| uart-1 | uart_16650 | 00002 | 
+--------+------------+--------+ 

在Python3我仍在使用unicode的D型。如果任何字符串都是字節,則prettyprint將顯示b

+0

嗨,非常感謝你!很好的答案,但是有沒有什麼方法可以用python中的實際表格顯示數據? – GoldenEagle

+0

實際表格?用細線劃分細胞? – hpaulj

+0

是的,細胞不必像excel那樣可移動或任何幻想。只想在表格中顯示信息。 – GoldenEagle

0

爲了避免打印b'xxx',試試這個:

print (', '.join(y.decode() for y in inst_rec[x])) 
+0

這個答案是正確的,但一些細節丟失:@ user3757208你正在面對[bytestrings](https://docs.python.org/ 3/library/stdtypes.html#bytes)和建議的[.decode()](https://docs.python.org/3/library/stdtypes.html#bytes.decode)方法你擺脫了那個b' '符號,因爲它不再是字節串。 – colidyre

相關問題