Last active
April 2, 2016 00:34
-
-
Save elisium-oat/f4b195c0fb615a7f951a170f585df7a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # SVG file to use -> https://upload.wikimedia.org/wikipedia/commons/9/91/Tableau_p%C3%A9riodique_des_%C3%A9l%C3%A9ments.svg | |
| # Use Pyhton 3.4 | |
| from xml.dom import minidom | |
| import json | |
| def main(): | |
| """THE main function.""" | |
| elements = {} | |
| with minidom.parse('./Tableau_périodique_des_éléments.svg') as dom: | |
| for e in dom.getElementsByTagName('text'): | |
| id = e.getAttribute('id') | |
| value = e.firstChild.nodeValue | |
| key = '' | |
| type = '' | |
| if 'T_e' in id and '‒' not in value: | |
| # Name | |
| if id[2] == '.': | |
| type = 'name' | |
| key = id.strip('fr.T_e').strip('x') | |
| value = value.capitalize() | |
| # Symbole | |
| if id.endswith('s'): | |
| type = 'symbole' | |
| key = id.strip('T_e').strip('s') | |
| # Number | |
| if id.endswith('n'): | |
| type = 'number' | |
| key = id.strip('T_e').strip('n') | |
| # Mass | |
| if id.endswith('m'): | |
| type = 'mass' | |
| key = id.strip('T_e').strip('m') | |
| value = value.strip('[').strip(']').replace(',', '.') | |
| # Verify if the key already exist | |
| if key not in elements: | |
| elements[key] = {} | |
| # Add the value | |
| elements[key][type] = value | |
| with open('elements.json', 'w') as f: | |
| f.write( | |
| json.dumps(elements, | |
| sort_keys=True, | |
| ensure_ascii=False, | |
| indent=2)) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment