python - 如何将 numpy ndarray 导出到字符串变量?

标签 python string numpy lxml elementtree

我正在尝试使用以下代码编写一个 xml 文件:

def make_xml(a_numpy_array):

    from lxml import etree as ET

    root = ET.Element('intersections')
    intersection = ET.SubElement(root, 'intersection')
    trace = ET.SubElement(intersection, 'trace')
    trace.text = a_numpy_array

    print ET.tostring(root, pretty_print=True, xml_declaration=True, encoding = 'utf-8')

.....

trace.text 需要一个字符串输入。我想在这里的 xml 文件中放入一个存储为 numpy ndarray 的二维数组。但我似乎无法将数据导出为字符串。 numpy.tostring 给我字节码,我该用它做什么?我想出的解决方案是将 ndarray 写入文本文件,然后将文本文件作为字符串读取,但我希望能够跳过写入文本文件。

最佳答案

你可以做到

trace.text = str(a_numpy_array)

有关更多选项,请参阅 numpy.array_strnumpy.array2string .

关于python - 如何将 numpy ndarray 导出到字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764131/

相关文章:

python - numpy数组: fast assign short array to large array with index

python - 续对数运算 : floor operator on run-length encoded terms

ios - 创建 UIButton 其标题的宽度

python - 在切片上使用 np.argwhere 时如何获取原始数组中的索引?

android - URI 生成器结果中有一个意外的冒号(Android 编程)

python - 打印随机文本中的计数(单词出现次数)(Print hackerearth)

python - boost::python 从 numpy.ndarray 返回值中提取 C++ 值

python - 如何使用PyTorch打印出每个类别的预测准确率?

python - Python 协程可以不用 await 或 yield 实现吗?

python - GraphDB-Python Sparql 查询返回 : http status (404)