我的第一个python小程序(替换文本中的标点符号为换行符)
2025-03-23 17:21:15 534 分享链接 开发笔记 python
import string
# 合并中文和英文标点符号
punctuations = string.punctuation + '!?。。,、;:“”‘’()〔〕【】﹃﹄「」﹁﹂—…-~《》〈〉'
def remove_punctuation_and_newline(text):
result = ""
for char in text:
if char in punctuations:
result += '\n'
else:
result += char
# 去除多余的空行
result = '\n'.join(line.strip() for line in result.split('\n') if line.strip())
return result
if __name__ == "__main__":
try:
# 读取文件
with open('a.txt', 'r', encoding='utf-8') as file:
input_text = file.read()
# 去除标点符号并换行
output_text = remove_punctuation_and_newline(input_text)
# 将结果写入新文件
with open('output.txt', 'w', encoding='utf-8') as output_file:
output_file.write(output_text)
print("处理完成,结果已保存到 output.txt 文件中。")
except FileNotFoundError:
print("错误:未找到 a.txt 文件,请检查文件路径和文件名。")
except Exception as e:
print(f"发生未知错误:{e}")

最近更新
- 2026-04-24 04:08
- 番茄古风(古言)小说角色按用途划分八大服饰大类
- 2026-04-24 03:30
- 番茄古风(古言)小说角色设定提示词【标准化填空模板】
- 2026-04-24 02:05
- 番茄古风(古言)小说五大题材的角色形象卡字段与下拉词库
- 2026-04-24 01:41
- 番茄古风(古言)小说女频阅读榜的常见类型有哪些?
- 2026-04-24 00:39
- 整理一套适配古风、AI 识别度高、适配你扣子工作流的唇形词库。
- 2026-04-23 17:24
- 公式:主体 + 动作 + 场景 你的豆包从此听话。
- 2026-04-18 13:43
- 推荐几种即梦AI常见的古风动漫绘画艺术风格
- 2026-04-16 13:48
- 北影教科书级|AI电影提示词精准运用方法|影视工业级落地流程
- 2026-04-14 00:25
- 最适合古风漫画非人类角色(神兽 / 器灵 / 精怪)的 AI 绘画主流风格
- 2026-04-14 00:10
- 常见的古言小说中的非人类角色设计有哪些