Last active
March 23, 2016 15:03
-
-
Save astanin/6b7f2b6d7b22095695a7 to your computer and use it in GitHub Desktop.
xmlize(), a simple function similar to tabulate, to generate xml tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import tabulate as T\n", | |
"import xml.etree.ElementTree as ET" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Planet Radius (km) Mass (x 10^29 kg)\n", | |
"-------- ------------- -------------------\n", | |
"Sun 696000 1.9891e+09\n", | |
"Earth 6371 5973.6\n", | |
"Moon 1737 73.5\n", | |
"Mars 3390 641.85\n" | |
] | |
} | |
], | |
"source": [ | |
"table = [[\"Sun\",696000,1989100000],[\"Earth\",6371,5973.6],\n", | |
" [\"Moon\",1737,73.5],[\"Mars\",3390,641.85]]\n", | |
"headers = [\"Planet\", \"Radius (km)\", \"Mass (x 10^29 kg)\"]\n", | |
"\n", | |
"print(T.tabulate(table, headers))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<table>\n", | |
" <headers>\n", | |
" <planet>Planet</planet>\n", | |
" <radius>Radius (km)</radius>\n", | |
" <mass>Mass (x 10^29 kg)</mass>\n", | |
" </headers>\n", | |
" <data>\n", | |
" <row>\n", | |
" <planet>Sun</planet>\n", | |
" <radius>696000</radius>\n", | |
" <mass>1989100000</mass>\n", | |
" </row>\n", | |
" <row>\n", | |
" <planet>Earth</planet>\n", | |
" <radius>6371</radius>\n", | |
" <mass>5973.6</mass>\n", | |
" </row>\n", | |
" <row>\n", | |
" <planet>Moon</planet>\n", | |
" <radius>1737</radius>\n", | |
" <mass>73.5</mass>\n", | |
" </row>\n", | |
" <row>\n", | |
" <planet>Mars</planet>\n", | |
" <radius>3390</radius>\n", | |
" <mass>641.85</mass>\n", | |
" </row>\n", | |
" </data>\n", | |
"</table>\n" | |
] | |
} | |
], | |
"source": [ | |
"def xmlize(list_of_lists, headers, str2tagname=lambda s: \"{!s}\".format(s)):\n", | |
" tagnames = [str2tagname(h) for h in headers]\n", | |
" root = ET.Element(\"table\")\n", | |
" headersList = ET.SubElement(root, \"headers\")\n", | |
" for h, tagname in zip(headers, tagnames):\n", | |
" hElem = ET.SubElement(headersList, tagname)\n", | |
" hElem.text = \"{!s}\".format(h)\n", | |
" dataElem = ET.SubElement(root, \"data\")\n", | |
" for row in list_of_lists:\n", | |
" rowElem = ET.SubElement(dataElem, \"row\")\n", | |
" for val, h, tagname in zip(row, headers, tagnames):\n", | |
" cellElem = ET.SubElement(rowElem, tagname)\n", | |
" cellElem.text = \"{!s}\".format(val)\n", | |
" return root\n", | |
"\n", | |
"\n", | |
"def indent(elem, level=0):\n", | |
" i = \"\\n\" + level*\" \"\n", | |
" if len(elem):\n", | |
" if not elem.text or not elem.text.strip():\n", | |
" elem.text = i + \" \"\n", | |
" if not elem.tail or not elem.tail.strip():\n", | |
" elem.tail = i\n", | |
" for elem in elem:\n", | |
" indent(elem, level+1)\n", | |
" if not elem.tail or not elem.tail.strip():\n", | |
" elem.tail = i\n", | |
" else:\n", | |
" if level and (not elem.tail or not elem.tail.strip()):\n", | |
" elem.tail = i\n", | |
"\n", | |
" \n", | |
"def first_word(s):\n", | |
" return s.split()[0].lower()\n", | |
"\n", | |
"\n", | |
"xml_table = xmlize(table, headers, str2tagname=first_word)\n", | |
"indent(xml_table)\n", | |
"ET.dump(xml_table)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment