This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/文本处理工具/表格整理.ipynb

97 lines
2.5 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dollared(string):\n",
" flag = True\n",
" for c in string:\n",
" if not c in \"1234567890.+-:[]()\":\n",
" flag = False\n",
" break\n",
" if flag:\n",
" string = \"$\" + string + \"$\"\n",
" return string\n",
"\n",
"\n",
"with open(\"临时文件/textfile.txt\", \"r\", encoding = \"utf8\") as textfile:\n",
" data = textfile.read().strip()\n",
"\n",
"data1 = \"\"\n",
"for c in data:\n",
" if 65296 <= ord(c) < 65306:\n",
" data1 += str(ord(c)-65296)\n",
" else:\n",
" data1 += c\n",
"data = data1\n",
"data = data.replace(\"\",\".\").replace(\"\",\":\")\n",
"elements = data.split(\"\\n\")\n",
"elements_per_line = int(elements.pop(-1)) #这里需要修改\n",
"contents = \"\\\\begin{center}\\n\\\\begin{tabular}{|\"\n",
"for i in range(elements_per_line):\n",
" contents += \"c|\"\n",
"contents += \"}\\n\"\n",
"contents += r\"\\hline\"+\"\\n\"\n",
"col = 1\n",
"for element in elements:\n",
" if col != 1:\n",
" contents += \" & \"\n",
" contents += dollared(element)\n",
" if col == elements_per_line:\n",
" contents += r\" \\\\ \\hline\"+\"\\n\"\n",
" col += 1\n",
" if col > elements_per_line:\n",
" col = 1\n",
"contents += \"\\\\end{tabular}\" + \"\\n\" + \"\\\\end{center}\"\n",
"\n",
"with open(\"临时文件/tablefile.txt\",\"w\",encoding = \"utf8\") as f:\n",
" f.write(contents)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pythontest",
"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.8.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}