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

相关推荐
Zlzxzw3 分钟前
End-to-End Autonomous Generation of Human Assembly Plans
python
Wellijohn32 分钟前
2000 个请求、30% 失败率:我把 Python 批量并发的限流和重试参数算了一遍
后端·python·性能优化
feixing_fx2 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
Logintern093 小时前
deep agents 多智能体模式——规划与委派
python·langchain
前端架构师-老李3 小时前
html教程:header、nav、main、footer 四个标签各管哪一块?
前端·html
烟雨空灵4 小时前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
wangruofeng4 小时前
怎么让你的项目更好看?用 UI Lab 练设计原则,给界面去 AI 味
前端·aigc·ai编程
2601_967980114 小时前
Python 办公自动化合集 ⑰:Excel自动计算——总和/平均分/最大值
python·pycharm·办公自动化·数据处理·excel自动计算
用户69371750013845 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
涛涛ing6 小时前
线上突发大面积白屏,排查一天后,我发现是 `JSON.parse` 背了锅
前端