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')
“桌子”是软的吗?一个2-D numpy数组?一个HTML'
表是什么意思?你的意思是数据库?如果是这样,哪个数据库?从那里你可以搜索'如何插入MYDB'。 – postelrich
你可能想看看'tabulate'模块。那就是如果你字面意思是一张桌子。该模块能够处理字典,列表,numpy数组和其他结构化数据,然后将它们格式化为HTML,降价,乳胶和纯文本查看。 [tabulate](https://pypi.python.org/pypi/tabulate) – Ajay
回答
您正在使用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'
)产生
简化显示and
为ASCII表显示
如果你想要的东西票友你需要指定显示工具 - HTML,RTF文本等
与加包
prettyprint
:产生
在Python3我仍在使用unicode的D型。如果任何字符串都是字节,则
prettyprint
将显示b
。来源
2015-10-07 00:06:21 hpaulj
嗨,非常感谢你!很好的答案,但是有没有什么方法可以用python中的实际表格显示数据? – GoldenEagle
实际表格?用细线划分细胞? – hpaulj
是的,细胞不必像excel那样可移动或任何幻想。只想在表格中显示信息。 – GoldenEagle
为了避免打印
b'xxx'
,试试这个:来源
2015-10-06 21:19:18
这个答案是正确的,但一些细节丢失:@ user3757208你正在面对[bytestrings](https://docs.python.org/ 3/library/stdtypes.html#bytes)和建议的[.decode()](https://docs.python.org/3/library/stdtypes.html#bytes.decode)方法你摆脱了那个b' '符号,因为它不再是字节串。 – colidyre
相关问题