{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os,re,json\n", "\n", "def form_decimals(string):\n", " string = re.sub(r\"[\\s]+\",r\"\\t\",string)\n", " numerals_list = [n for n in string.split(\"\\t\") if len(n)>0]\n", " for i in range(len(numerals_list)):\n", " numeral = numerals_list[i]\n", " str_numeral = \"%.3f\" %float(numeral)\n", " numerals_list[i] = str_numeral\n", " return \"\\t\".join(numerals_list)\n", "\n", "with open(\"文本文件/手动统计结果.txt\",\"r\",encoding = \"utf8\") as f:\n", " data = f.read()\n", "\n", "blocks = re.findall(r\"\\[BEGIN\\]([\\s\\S]*?)\\[END\\]\",data)\n", "\n", "results_dict = {}\n", "for block in blocks:\n", " temp_list = [l.strip() for l in block.split(\"\\n\") if l.strip() != \"\"] \n", " for line in temp_list:\n", " if line[:2] == \"##\":\n", " date = line[2:].strip()\n", " elif line[:2] == \"**\":\n", " current_class = line[2:].strip()\n", " else:\n", " separating_pos = re.search(\"\\s\",line).span(0)[0]\n", " if not line[:separating_pos].zfill(6) in results_dict:\n", " results_dict[line[:separating_pos].zfill(6)] = [date + \"\\t\" + current_class + \"\\t\" + form_decimals(re.sub(\"\\s+?\",\"\\t\",line[separating_pos:])).strip()]\n", " else:\n", " results_dict[line[:separating_pos].zfill(6)].append(date + \"\\t\" + current_class + \"\\t\" + form_decimals(re.sub(\"\\s+?\",\"\\t\",line[separating_pos:])).strip())\n", "\n", "output_data = \"usages\\n\"\n", "for id in results_dict:\n", " output_data += id + \"\\n\"\n", " output_data += \"\\n\".join(results_dict[id])\n", " output_data += \"\\n\\n\"\n", "\n", "with open(\"文本文件/metadata.txt\",\"w\",encoding = \"utf8\") as f:\n", " f.write(output_data)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.15 ('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.9.15" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93" } } }, "nbformat": 4, "nbformat_minor": 2 }