我的第一个python小程序(替换文本中的标点符号为换行符)
2025-03-23 17:21:15 197 分享链接 开发笔记 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}")
最近更新
- 2025-07-18 00:20
- 详细说明在Python中如何使用元组作为函数的返回值。
- 2025-07-18 00:15
- 在图形用户界面(GUI)编程中 启动一个新的线程可以避免阻塞 GUI 线程。
- 2025-07-17 20:11
- MoviePy 是一个用于视频编辑的 Python 库,依赖Ffmpeg运行,适合无法直接使用 FFmpeg 的场景。
- 2025-07-16 19:31
- 使用FFmpeg 做混剪软件的命令解释
- 2025-07-15 16:13
- 抖音直播下载的文件flv,现在用播放器播放不了的解决办法。
- 2025-07-13 16:32
- 使用OpenCV 给豆包生成的图片去除水印的命令
- 2025-07-13 15:57
- 使用FFmpeg 给豆包生成的图片去除水印的命令
- 2025-07-07 16:49
- FFmpeg 的 subtitles 滤镜 颜色编码解析
- 2025-07-07 16:41
- FFmpeg 添加黄色边框黑色自定义字体的命令说明
- 2025-07-07 15:35
- 剪映专业版的字体下载位置在哪里,剪映专业版的字体下载去哪里了?