Created
September 20, 2016 12:49
-
-
Save kurozumi/01ee28c89f59309d516b4be06523a632 to your computer and use it in GitHub Desktop.
【Python】XMLを辞書に変換後、JSONに変換する方法
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": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# xmltodictモジュールをインポート\n", | |
"import xmltodict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"xml = \"\"\"\n", | |
"<mydocument has=\"an attribute\">\n", | |
" <and>\n", | |
" <many>elements</many>\n", | |
" <many>more elements</many>\n", | |
" </and>\n", | |
" <plus a=\"complex\">\n", | |
" element as well\n", | |
" </plus>\n", | |
"</mydocument>\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"OrderedDict([('mydocument', OrderedDict([('@has', 'an attribute'), ('and', OrderedDict([('many', ['elements', 'more elements'])])), ('plus', OrderedDict([('@a', 'complex'), ('#text', 'element as well')]))]))])\n" | |
] | |
} | |
], | |
"source": [ | |
"# xmlをパースして辞書に変換\n", | |
"dict = xmltodict.parse(xml)\n", | |
"print(dict)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# jsonモジュールをインポート\n", | |
"import json" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" \"mydocument\": {\n", | |
" \"@has\": \"an attribute\",\n", | |
" \"and\": {\n", | |
" \"many\": [\n", | |
" \"elements\",\n", | |
" \"more elements\"\n", | |
" ]\n", | |
" },\n", | |
" \"plus\": {\n", | |
" \"@a\": \"complex\",\n", | |
" \"#text\": \"element as well\"\n", | |
" }\n", | |
" }\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"# 辞書をjsonに変換\n", | |
"json = json.dumps(dict, indent=4)\n", | |
"print(json)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment