{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "652d02d5-2c81-4607-a8df-8e14876bef28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "处理完成!\n" ] } ], "source": [ "import os\n", "import re\n", "\n", "# 指定目录路径\n", "directory = './'\n", "\n", "# 遍历目录下所有的txt文件\n", "for filename in os.listdir(directory):\n", " if filename.endswith('.txt'):\n", " file_path = os.path.join(directory, filename)\n", " \n", " with open(file_path, 'r', encoding='utf-8') as file:\n", " content = file.read()\n", " \n", " # 移除大括号、引号和单引号\n", " content = re.sub(r'[{}\\'\"]', '', content) # 移除大括号、单引号和引号\n", " content = re.sub(r\"':\", '', content) # 移除特定字符串\n", "\n", " with open(file_path, 'w', encoding='utf-8') as file:\n", " file.write(content)\n", "\n", "print(\"处理完成!\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }