如何将数组中的记录放入 python 中的 table?
How can I get records from an array into a table in python?
我有一个 xml 文件,其中包含一些我正在提取并放置在 numpy 记录数组中的数据。我打印数组,我看到数据位于正确的位置。我想知道如何在我的 numpy 记录数组中获取该信息并将其放在 table 中。另外,我在打印记录时收到字母 b,我该如何解决?
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')
要避免打印 b'xxx'
,试试这个:
print (', '.join(y.decode() for y in inst_rec[x]))
您正在使用 Python3,它使用 unicode 字符串。它显示带有 b
的字节字符串。 xml文件也可以是字节,例如encoding='UTF-8'
.
您可以通过在打印前将字符串传递给 decode()
来摆脱 b
。
更多关于在 Py3 中编写 csv
文件
在我的测试中,我可以通过使 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
和
1703:~/mypy$ cat test.out
uart-0, uart_16550,00001
uart-1, uart_16650,00002
作为 ASCII table 显示
# formatted row by row print
print('----------------------------------------')
for rec in inst_rec:
print('| %20s | %20s | %5s |'%tuple(rec))
print('---------------------------------------')
如果您想要更高级的东西,您需要指定显示工具 - html、富文本等
添加包 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 dtype。如果任何字符串是字节,prettyprint
将显示 b
。
我有一个 xml 文件,其中包含一些我正在提取并放置在 numpy 记录数组中的数据。我打印数组,我看到数据位于正确的位置。我想知道如何在我的 numpy 记录数组中获取该信息并将其放在 table 中。另外,我在打印记录时收到字母 b,我该如何解决?
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')
要避免打印 b'xxx'
,试试这个:
print (', '.join(y.decode() for y in inst_rec[x]))
您正在使用 Python3,它使用 unicode 字符串。它显示带有 b
的字节字符串。 xml文件也可以是字节,例如encoding='UTF-8'
.
您可以通过在打印前将字符串传递给 decode()
来摆脱 b
。
更多关于在 Py3 中编写 csv
文件
在我的测试中,我可以通过使 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
和
1703:~/mypy$ cat test.out
uart-0, uart_16550,00001
uart-1, uart_16650,00002
作为 ASCII table 显示
# formatted row by row print
print('----------------------------------------')
for rec in inst_rec:
print('| %20s | %20s | %5s |'%tuple(rec))
print('---------------------------------------')
如果您想要更高级的东西,您需要指定显示工具 - html、富文本等
添加包 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 dtype。如果任何字符串是字节,prettyprint
将显示 b
。