Created
January 21, 2024 15:37
-
-
Save rwcitek/e85ae521246834821c04ad7ce4db22ae to your computer and use it in GitHub Desktop.
Create UTF-8 Dictionary
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, | |
"id": "5dced423-9ff4-4343-85b4-4efaae87a53a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%capture\n", | |
"!pip install lxml pandas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "97c8de8f-eb6f-4847-adad-3e155e0e9e58", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "bc83a338-bcd3-4ea2-8474-45d9ada3cc6c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Specify the URL of the website containing the table\n", | |
"url = 'https://www.utf8-chartable.de/unicode-utf8-table.pl?number=1024&names=-'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "f97f0582-efe1-4db5-bdfd-ad88bbaabef2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Use pandas to read the table from the specified URL\n", | |
"tables = pd.read_html(url)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "9ba697b4-70f0-41c4-b81d-c8cdda895818", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# How many tables?\n", | |
"len(tables)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "134902fe-9cba-4cc3-948e-936c5c31278e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>Unicode code point</th>\n", | |
" <th>Char</th>\n", | |
" <th>UTF-8</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>U+0000</td>\n", | |
" <td>NaN</td>\n", | |
" <td>00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>U+0001</td>\n", | |
" <td>NaN</td>\n", | |
" <td>01</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>U+0002</td>\n", | |
" <td>NaN</td>\n", | |
" <td>02</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>U+0003</td>\n", | |
" <td>NaN</td>\n", | |
" <td>03</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>U+0004</td>\n", | |
" <td>NaN</td>\n", | |
" <td>04</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1019</th>\n", | |
" <td>U+03FB</td>\n", | |
" <td>ϻ</td>\n", | |
" <td>cf bb</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1020</th>\n", | |
" <td>U+03FC</td>\n", | |
" <td>ϼ</td>\n", | |
" <td>cf bc</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1021</th>\n", | |
" <td>U+03FD</td>\n", | |
" <td>Ͻ</td>\n", | |
" <td>cf bd</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1022</th>\n", | |
" <td>U+03FE</td>\n", | |
" <td>Ͼ</td>\n", | |
" <td>cf be</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1023</th>\n", | |
" <td>U+03FF</td>\n", | |
" <td>Ͽ</td>\n", | |
" <td>cf bf</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>1024 rows × 3 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Unicode code point Char UTF-8\n", | |
"0 U+0000 NaN 00\n", | |
"1 U+0001 NaN 01\n", | |
"2 U+0002 NaN 02\n", | |
"3 U+0003 NaN 03\n", | |
"4 U+0004 NaN 04\n", | |
"... ... ... ...\n", | |
"1019 U+03FB ϻ cf bb\n", | |
"1020 U+03FC ϼ cf bc\n", | |
"1021 U+03FD Ͻ cf bd\n", | |
"1022 U+03FE Ͼ cf be\n", | |
"1023 U+03FF Ͽ cf bf\n", | |
"\n", | |
"[1024 rows x 3 columns]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Work with the third table\n", | |
"df = tables[2]\n", | |
"df.columns = ['Unicode code point', 'Char', 'UTF-8']\n", | |
"df\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "49517050-ed7e-4516-8858-cfba3fd0d8a8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create dictionary\n", | |
"filter = df[\"UTF-8\"].str.contains(r'^c[3-9a-f]')\n", | |
"utf8_char_dict = df[ filter ].set_index('UTF-8')['Char'].to_dict()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "d2eeab76-8ffd-4dc9-babf-ddcd6bb9d763", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'c3 80': 'À',\n", | |
" 'c3 81': 'Á',\n", | |
" 'c3 82': 'Â',\n", | |
" 'c3 83': 'Ã',\n", | |
" 'c3 84': 'Ä',\n", | |
" 'c3 85': 'Å',\n", | |
" 'c3 86': 'Æ',\n", | |
" 'c3 87': 'Ç',\n", | |
" 'c3 88': 'È',\n", | |
" 'c3 89': 'É'}" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Display first few items of dictionary\n", | |
"dict(list(utf8_char_dict.items())[:10])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "aeabd281-c08c-4635-9222-19c511e6234d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.10.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment