2015-10-06 80 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