0

I have an application that takes an xml file as input and converts this data into a specific data structure. I would like to write a test for this application, but instead of using an external xml file I would like to define the xml data inside the test file and then pass this data to the function, so originally my idea was to do something like this:

data = pd.DataFrame([#insert data here])
in_memory_xml = io.BytesIO()
xml_file = original.to_xml(in_memory_xml)
my_function(xml_file)

However, pandas DataFrame objects do not have a "to_xml" function, so the xml data needs to be defined differently.Is there good way to solve this problem that doesn't involve the use of an external xml file?

  • Please set up a [specific example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) with data and desired output. Right now this reads too general. – Parfait Feb 25 '20 at 15:15

2 Answers2

0

It can be done by just converting the string to xml using lxml: https://kite.com/python/examples/5415/lxml-load-xml-from-a-string-into-an-%60elementtree%60

0

You can try the below:

data=pd.read_csv('temps.csv',delimiter=r"\s+")

def myfunction(row):
    myxml = ['<item>']
    for field in row.index:
        myxml.append('  <field name="{0}">{1}</field>'.format(field, row[field]))
    myxml.append('</item>')
    return '\n'.join(myxml)

final_xml='\n'.join(data.apply(myfunction, axis=1))
print(final_xml)
Jai
  • 819
  • 7
  • 17