-2

I want to convert dictionary to xml in python, I am using dicttoxml for that. My code looks like this:

>>> import os
>>> filepath=os.path.normpath('C:\\Users\\\\Desktop\\abc\\bc.txt')
>>> with open(filepath,'r') as f:
...     for line in f:
...             x=line.split(':')
...             x[-1]=x[-1].strip()
...             a=x[0]
...             b=x[1]
...             d[a]=b
...     xml = dicttoxml(d,custom_root='doc',attr_type=False)
...     xml2 = parseString(xml)
...     xml3 = xml2.toprettyxml()
...     print( xml3 )

The output is like this:

<?xml version="1.0" ?>
<doc>
        <key name="product/productId">B000GKXY34</key>
        <key name="product/title">Nun Chuck, Novelty Nun Toss Toy</key>
        <key name="product/price">17.99</key>
        <key name="review/userId">ADX8VLDUOL7BG</key>
        <key name="review/profileName">M. Gingras</key>
        <key name="review/helpfulness">0/0</key>
        <key name="review/score">5.0</key>
        <key name="review/time">1262304000</key>
        <key name="review/summary">Great fun!</key>
        <key name="review/text">Got these last Christmas as a gag gift. They are great fun, but obviously this is not a toy that lasts!</key>
</doc>

but I want to replace the key name with field name.

<field name="product/productId">B000GKXY34</field>

This is the dictionary generated by the code:

{'product/productId': 'B000GKXY34', 'product/title': 'Nun Chuck, Novelty Nun Toss Toy', 'product/price': '17.99', 'review/userId': 'ADX8VLDUOL7BG', 'review/profileName': 'M. Gingras', 'review/helpfulness': '0/0', 'review/score': '5.0', 'review/time': '1262304000', 'review/summary': 'Great fun!', 'review/text': 'Got these last Christmas as a gag gift. They are great fun, but obviously this is not a toy that lasts!'}

And also i want to write that xml in a new file,i am trying with write function but its not working:

with open ('filename','w') as output:
            output.write(xml3)
sultan
  • 115
  • 2
  • 11
  • Please [edit] your question and provide either the contents of the file being read, _or_ the dictionary that your code produces and sends to the `dicttoxml()` function. – martineau Oct 27 '17 at 15:43

2 Answers2

1

According to dicttoxml documentation:

Define Custom Item Names

Starting in version 1.7, if you don’t want item elements in a list to be called ‘item’, you can specify the element name using a function that takes the parent element name (i.e. the list name) as an argument.

>>> import dicttoxml
>>> obj = {u'mylist': [u'foo', u'bar', u'baz'], u'mydict': {u'foo': u'bar', u'baz': 1}, u'ok': True}
>>> my_item_func = lambda x: 'list_item'
>>> xml = dicttoxml.dicttoxml(obj, item_func=my_item_func)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <mydict type="dict">
    <foo type="str">bar</foo>
    <baz type="int">1</baz>
  </mydict>
  <mylist type="list">
    <list_item type="str">foo</list_item>
    <list_item type="str">bar</list_item>
    <list_item type="str">baz</list_item>
  </mylist>
  <ok type="bool">True</ok>
</root>
martineau
  • 99,260
  • 22
  • 139
  • 249
Javapocalypse
  • 1,725
  • 14
  • 21
0

From the documentation, we have to use a function to provide the custom key name for the xml.

Reference - dicttoxml github

>>> import os
>>> filepath=os.path.normpath('C:\\Users\\\\Desktop\\abc\\bc.txt')
>>> with open(filepath,'r') as f:
...     for line in f:
...             x=line.split(':')
...             x[-1]=x[-1].strip()
...             a=x[0]
...             b=x[1]
...             d[a]=b
...     returnfield = lambda x: 'field'
...     xml = dicttoxml(d,custom_root='doc',attr_type=False, item_func=returnfield)
...     xml2 = parseString(xml)
...     xml3 = xml2.toprettyxml()
...     print( xml3 )

Output is :

<?xml version="1.0" ?>
<doc>
        <field name="product/productId">B000GKXY34</field>
        <field name="product/title">Nun Chuck, Novelty Nun Toss Toy</field>
        <field name="product/price">17.99</field>
        <field name="review/userId">ADX8VLDUOL7BG</field>
        <field name="review/profileName">M. Gingras</field>
        <field name="review/helpfulness">0/0</field>
        <field name="review/score">5.0</field>
        <field name="review/time">1262304000</field>
        <field name="review/summary">Great fun!</field>
        <field name="review/text">Got these last Christmas as a gag gift. They are great fun, but obviously this is not a toy that lasts!</field>
</doc>