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...

相关推荐
lizhongxuan4 小时前
Claude Code 防上下文爆炸:源码级深度解析
前端·后端
紫金修道4 小时前
【DeepAgent】概述
开发语言·数据库·python
书到用时方恨少!4 小时前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
Warson_L4 小时前
Python 常用内置标准库
python
Warson_L5 小时前
Python 函数的艺术 (Functions)
python
Warson_L5 小时前
Python 流程控制与逻辑
后端·python
long_songs5 小时前
手柄键盘映射器【github链接见文末 】
python·游戏·计算机外设·pygame·软件推荐·手柄映射键盘
必然秃头5 小时前
Python 环境安装及项目构建指南
python
Warson_L5 小时前
Python 四大组合数据类型 (Collection Types)
后端·python
廋到被风吹走5 小时前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python