{"id":2762,"date":"2022-01-22T20:07:18","date_gmt":"2022-01-22T20:07:18","guid":{"rendered":"https:\/\/dft.wiki\/?p=2762"},"modified":"2022-01-22T20:10:55","modified_gmt":"2022-01-22T20:10:55","slug":"working-with-csv-and-xml-in-python","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2762","title":{"rendered":"Working with CSV and XML in Python"},"content":{"rendered":"<p>In the following example, the Python code will create a dictionary with the headers (fields on the first line) from the CSV input file and recreate the same structure in the XML output file.<\/p>\n<p><strong>Input file:<\/strong><\/p>\n<pre>\"Username\",\"Identifier\",\"First Name\",\"Last Name\"\r\n\"booker12\",\"9012\",\"Rachel\",\"Booker\"\r\n\"grey07\",\"2070\",\"Laura\",\"Grey\"\r\n\"johnson81\",\"4081\",\"Craig\",\"Johnson\"\r\n\"jenkins46\",\"9346\",\"Mary\",\"Jenkins\"\r\n\"smith79\",\"5079\",\"Jamie\",\"Smith\"<\/pre>\n<p><strong>Converter script:<\/strong><\/p>\n<pre>#!\/bin\/python\r\n# Converter: CSV to XML\r\n\r\nimport csv\r\nimport xml.etree.ElementTree as xml\r\n\r\ninput = 'input.csv'\r\noutput = 'output.xml'\r\n\r\n# opening the CSV input\r\nfile = open(input)\r\nreader = csv.reader(file, delimiter=',', quotechar='\"')\r\nheaders = next(reader)\r\n\r\n# creating the dictionary\r\nindexes = {}\r\nfor header in headers:\r\n    indexes[header] = headers.index(header)\r\n\r\n# creating the XML structure\r\nroot = xml.Element(\"Collection\")\r\n\r\nid = 0\r\nfor item in reader:\r\n    id = id + 1\r\n\r\n    # creating each entry (element)\r\n    entry = xml.Element(\"Entry\")\r\n    \r\n    # setting an attribute to the entry\r\n    entry.set('id',str(id))\r\n    root.append(entry)\r\n\r\n    # adding each property to the entry\r\n    for variable in indexes:\r\n        entry_property = xml.SubElement(entry, variable)\r\n        entry_property.text = item[indexes[variable]]\r\n\r\n# writing the XML structure to a file\r\ntree = xml.ElementTree(root)\r\nwith open(output, \"wb\") as output_file:\r\n    tree.write(output_file)\r\n\r\nexit()<\/pre>\n<p>This script can be executed giving it as an argument to the Python interpreter:<\/p>\n<pre>python converter.py<\/pre>\n<p>Or by marking it as executable and executing it directly:<\/p>\n<pre>chmod +x converter.py\r\n.\/converter.py<\/pre>\n<p><strong>The output file will look like this:<\/strong><\/p>\n<pre>&lt;Collection&gt;\r\n  &lt;Entry id=\"1\"&gt;\r\n    &lt;Username&gt;booker12&lt;\/Username&gt;\r\n    &lt;First Name&gt;Rachel&lt;\/First Name&gt;\r\n    &lt;Identifier&gt;9012&lt;\/Identifier&gt;\r\n    &lt;Last Name&gt;Booker&lt;\/Last Name&gt;\r\n  &lt;\/Entry&gt;\r\n  &lt;Entry id=\"2\"&gt;\r\n    &lt;Username&gt;grey07&lt;\/Username&gt;\r\n    &lt;First Name&gt;Laura&lt;\/First Name&gt;\r\n    &lt;Identifier&gt;2070&lt;\/Identifier&gt;\r\n    &lt;Last Name&gt;Grey&lt;\/Last Name&gt;\r\n  &lt;\/Entry&gt;\r\n  &lt;Entry id=\"3\"&gt;\r\n    &lt;Username&gt;johnson81&lt;\/Username&gt;\r\n    &lt;First Name&gt;Craig&lt;\/First Name&gt;\r\n    &lt;Identifier&gt;4081&lt;\/Identifier&gt;\r\n    &lt;Last Name&gt;Johnson&lt;\/Last Name&gt;\r\n  &lt;\/Entry&gt;\r\n  &lt;Entry id=\"4\"&gt;\r\n    &lt;Username&gt;jenkins46&lt;\/Username&gt;\r\n    &lt;First Name&gt;Mary&lt;\/First Name&gt;\r\n    &lt;Identifier&gt;9346&lt;\/Identifier&gt;\r\n    &lt;Last Name&gt;Jenkins&lt;\/Last Name&gt;\r\n  &lt;\/Entry&gt;\r\n  &lt;Entry id=\"5\"&gt;\r\n    &lt;Username&gt;smith79&lt;\/Username&gt;\r\n    &lt;First Name&gt;Jamie&lt;\/First Name&gt;\r\n    &lt;Identifier&gt;5079&lt;\/Identifier&gt;\r\n    &lt;Last Name&gt;Smith&lt;\/Last Name&gt;\r\n  &lt;\/Entry&gt;\r\n&lt;\/Collection&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the following example, the Python code will create a dictionary with the headers (fields [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2762","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2762"}],"version-history":[{"count":5,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2762\/revisions"}],"predecessor-version":[{"id":2767,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2762\/revisions\/2767"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}