python做文本切割

bash 复制代码
def split_text(input_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        text = f.read()

    chunks = [text[i:i+1500] for i in range(0, len(text), 1500)]

    for i, chunk in enumerate(chunks, 1):
        with open(f'{i}.txt', 'w', encoding='utf-8') as f:
            f.write(chunk)

input_file = 'text.txt'  # 请将 'text.txt' 替换成你的实际文件名
split_text(input_file)

把文章切割为1500字的一段,分别存为1.txt、2.txt...

相关推荐
昔我往昔2 小时前
LangChain_RAG_学习小结
后端·python
张张123y2 小时前
后端第六课:Python 语法速通(0 基础入门,AI 应用开发的第一门语言)
开发语言·人工智能·python·ai·后端开发
@PHARAOH2 小时前
WHAT - Vite 8(Rolldown 底层构建引擎升级)
前端·架构
小马不起床2 小时前
Django 后端实战指南_10:用户扩展档案信息表
python·django·sqlite
Want5952 小时前
Python跳动的爱心①
开发语言·python·数学建模
SamChan902 小时前
PDF 翻译后 1,250 变成 1.250,是错了吗?用 Python 核对数字格式
python·pdf·ocr·数据校验·文档翻译
阿Q的AI笔记2 小时前
从ReAct到GRPO、从OpenHands到SWE-agent:Agent完整知识体系全拆解
前端·react.js·前端框架
Yolanda_20222 小时前
30.利用GPU训练(一)
python
计算机魔术师2 小时前
从六个月路线图到JIT规划:代码评审不只是审代码
前端
迅猛龙办公室2 小时前
Python实现PM空气质量提醒
服务器·前端·python