我的第一个python小程序(替换文本中的标点符号为换行符)
2025-03-23 17:21:15 498 分享链接 开发笔记 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-03-16 18:06
- 故事文本分镜分析与拆解模板
- 2026-03-15 00:37
- 文生图提示词优化技巧:具象化描述、要有画面感
- 2026-03-12 17:04
- 除了国风动漫还有哪些常见的动漫风格适合 AI 绘画?
- 2026-03-12 16:35
- 水墨写实、水墨写意有什么区别,还有哪些水墨家族?
- 2026-03-12 16:22
- 分享5个治愈系漫剧高频场景提示词,包括(色调、光影、风格、质感)
- 2026-03-12 16:06
- 分享10个宅斗漫剧高频场景提示词,包括(色调、光影、风格、质感)
- 2026-03-12 15:56
- 分享10个古言漫剧高频场景提示词,包括(色调、光影、风格、质感)
- 2026-03-10 19:11
- 按人物主体公式写10个与李白同时期不同诗人不同环境的提示词(唯美古风)
- 2026-03-10 18:58
- 即梦唯美古风专用、可直接复制粘贴的常用词库
- 2026-03-10 18:44
- 即梦文生图提示词12套主体完整公式(全品类、无遗漏)