我的第一个python小程序(替换文本中的标点符号为换行符)
2025-03-23 17:21:15 586 分享链接 开发笔记 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-06-17 01:39
- AI主体:50条20岁东亚女模特外貌提示词汇总
- 2026-06-17 00:20
- AI主体:50条28岁东亚男模特外貌提示词汇总
- 2026-06-16 18:41
- 通过豆包生成20种今年夏天主流穿搭风格的AI女装模特绘画提示词
- 2026-06-16 17:34
- 通过豆包生成20种主流穿搭风格的AI男装模特绘画提示词
- 2026-06-11 00:22
- 通过豆包完美复刻任意图片只需要两步
- 2026-06-10 17:55
- 在人物主体提示词中如何优化穿搭细节的描述?
- 2026-06-10 17:46
- 通过豆包生成10种不同风格的AI女装模特绘画提示词 v1.6
- 2026-06-10 17:19
- 通过豆包生成10种不同风格的AI女装模特绘画提示词 v1.5
- 2026-06-10 17:08
- 通过豆包生成10种不同风格的AI女装模特绘画提示词 v1.4
- 2026-06-10 17:01
- 通过豆包生成10种不同风格的AI女装模特绘画提示词 v1.3