How to export numpy ndarray to a string variable?
elementtree, lxml, numpy, python, string
Solution
You can do
trace.text = str(a_numpy_array)
For more options, see `numpy.array_str` and `numpy.array2string`.
Problem
I am trying to write an xml file using the code below: ``` 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` expects a string input. I want to put in the xml file here a 2D array stored as a numpy ndarray. But I cannot seem to be able to export the data to a string. `numpy.tostring` gives me bytecode, what do I do with that? the solution ive come up with is to write the ndarray to a text file and then read the text file as a string but I would like to be able to skip writing a text file.