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

相关推荐
博观而约取厚积而薄发6 分钟前
Pytest 从入门到精通,一篇就够(超详细实战教程)
python·测试工具·单元测试·自动化·pytest
2601_963771378 分钟前
WordPress SEO Guide: Boost Rankings Without Technical Jargon
前端·php
李顿波9 分钟前
Chrome 扩展程序 MV3 架构组件概览
前端·chrome·浏览器插件
imzed17 分钟前
使用 Playwright + Pytest 构建 Web UI 自动化测试框架
python·自动化·pytest
普通网友22 分钟前
pytest一些常见的插件
开发语言·python·pytest
KingCodeHan23 分钟前
ESLint 完全指南:从入门到精通
前端
时空系30 分钟前
Python 高性能高压缩打包器 —— 基于 JianPy 语义分析引擎
python
GHL28427109039 分钟前
安装chrome浏览器
前端·chrome
谙忆10241 小时前
WebCodecs 实战:用 ImageDecoder 和 VideoFrame 在浏览器里做硬件加速的帧处理
前端