Files
everyone-can-use-english/1000-hours/public/jupyter-notebooks/rename.ipynb
2024-08-22 17:36:26 +08:00

141 lines
6.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: './sounds-of-american-english'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 15\u001b[0m\n\u001b[1;32m 13\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m3.2.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# filename start with name\u001b[39;00m\n\u001b[0;32m---> 15\u001b[0m md_files \u001b[38;5;241m=\u001b[39m [f \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlistdir\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m./sounds-of-american-english\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mif\u001b[39;00m f\u001b[38;5;241m.\u001b[39mstartswith(name) \u001b[38;5;129;01mand\u001b[39;00m f\u001b[38;5;241m.\u001b[39mendswith(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.md\u001b[39m\u001b[38;5;124m'\u001b[39m)]\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28mprint\u001b[39m(md_files)\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: './sounds-of-american-english'"
]
}
],
"source": [
"import os\n",
"\n",
"# get all md file name start wtih 3.2.\n",
"md_files = [f for f in os.listdir('.') if f.startswith('3.2.') and f.endswith('.md')]\n",
"for f in md_files:\n",
" # get string between 3.2. and -\n",
" order = f.split('3.2.')[1].split('-')[0]\n",
" new_name = f'3.2.{str(int(order)-1)}-{f.split(\"-\")[1]}'\n",
" # rename file\n",
" os.rename(f, new_name)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" {\n",
" text: \"3.2.8. tʃ/dʒ\",\n",
" link: \"/sounds-of-american-english/3.2.8-tʃdʒ\",\n",
" },\n",
" {\n",
" text: \"3.2.9. tr/dr\",\n",
" link: \"/sounds-of-american-english/3.2.9-trdr\",\n",
" },\n",
" {\n",
" text: \"3.2.10. ts/dz\",\n",
" link: \"/sounds-of-american-english/3.2.10-tsdz\",\n",
" },\n",
" {\n",
" text: \"3.2.11. m, n, ŋ\",\n",
" link: \"/sounds-of-american-english/3.2.11-mnŋ\",\n",
" },\n",
" {\n",
" text: \"3.2.12. l, r\",\n",
" link: \"/sounds-of-american-english/3.2.12-lr\",\n",
" },\n",
" {\n",
" text: \"3.2.13. w, j\",\n",
" link: \"/sounds-of-american-english/3.2.13-wj\",\n",
" },\n",
" ]\n",
" },\n",
"\n"
]
}
],
"source": [
"text = \"\"\"\n",
" {\n",
" text: \"2.2.10. tʃ/dʒ\",\n",
" link: \"/sounds-of-american-english/2.2.10-tʃdʒ\",\n",
" },\n",
" {\n",
" text: \"2.2.11. tr/dr\",\n",
" link: \"/sounds-of-american-english/2.2.11-trdr\",\n",
" },\n",
" {\n",
" text: \"2.2.12. ts/dz\",\n",
" link: \"/sounds-of-american-english/2.2.12-tsdz\",\n",
" },\n",
" {\n",
" text: \"2.2.13. m, n, ŋ\",\n",
" link: \"/sounds-of-american-english/2.2.13-mnŋ\",\n",
" },\n",
" {\n",
" text: \"2.2.14. l, r\",\n",
" link: \"/sounds-of-american-english/2.2.14-lr\",\n",
" },\n",
" {\n",
" text: \"2.2.15. w, j\",\n",
" link: \"/sounds-of-american-english/2.2.15-wj\",\n",
" },\n",
" ]\n",
" },\n",
"\"\"\"\n",
"\n",
"lines = text.split('\\n')\n",
"for line in lines:\n",
" if \"2.2.\" in line:\n",
" order = line.split('2.2.')[1].split('.')[0]\n",
" if len(order) <= 2:\n",
" new_order = str(int(order)-2)\n",
" line = line.replace(f'2.2.{order}', f'2.2.{new_order}')\n",
" order = line.split('2.2.')[1].split('-')[0]\n",
" if len(order) <= 2:\n",
" new_order = str(int(order)-2)\n",
" line = line.replace(f'2.2.{order}', f'2.2.{new_order}')\n",
" line = line.replace('2.2.', '3.2.')\n",
" print(line) \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}