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

相关推荐
2601_9620973622 分钟前
GraphQL,Grafana和Dash
python·grafana·数据可视化·graphql·dash
_阿南_6 小时前
项目中新增给AI制定的代码规范
前端·程序员
名字还没想好☜7 小时前
React 实现暗黑模式切换:localStorage 持久化、SSR 首屏闪烁与跟随系统主题
前端·javascript·react.js·ecmascript·react·next.js
console.log('npc')8 小时前
Git 冲突与 AI 协助指南
前端·人工智能·git·大模型
benchmark_cc8 小时前
REST API 和 Python SDK 应该怎么选?量化交易数据接口选型实战
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
爱学堂IT分享9 小时前
Cesium可视化系统实战课程-Cesium教程学习
前端
2601_962382439 小时前
python快乐编程网络爬虫课后答案
python·网络爬虫·学习工具·课后答案·刷题app
糖墨夕9 小时前
理解大语言模型:Agent 的“大脑”
前端·agent
Rain的Java大神之路9 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
鹏哥带你干乡墅10 小时前
优选乡墅赋能培训平台如何帮助提升乡村建设?
大数据·人工智能·python