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

相关推荐
To_OC3 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
To_OC3 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
聪明蛋子哟4 小时前
Stagehand v3多语言SDK:Python/Go/Rust/Java下的浏览器自动化统一方案
python·golang·rust
今天AI了吗5 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
风月说与山鬼5 小时前
三、大括号语法
前端·react.js
卷无止境5 小时前
Windows 上丝滑开发 Python,并稳定构建 Docker 镜像
后端·python·docker
kyriewen5 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
TELL5215 小时前
selenium webdriver 第二次初始化的异常
开发语言·python
Csvn6 小时前
🐍 Day 4: Python 控制流 — 条件、循环与推导式的艺术
后端·python