python分割一个文本为多个文本的方法


Posted in Python onJuly 22, 2019

本文实例为大家分享了python分割一个文本为多个文本,供大家参考,具体内容如下

# load file
# for each row
## if match
## output
 
def main():
 file_source = './reading_questions.txt'
 #target_dir = ''
 file_in = open(file_source,'r')
 template_str = 'TARGET'
 
 outfilename = './head.txt'
 output_content = ''
 while 1:
 line = file_in.readline()
 if not line:
 break
 
 if line.find(template_str) != -1:
 write_file(outfilename,output_content)
 outfilename = './'+line+'.txt' # output file tile
 output_content = ''
 else:
 output_content += line # append 
 write_file(outfilename,output_content) #for the last file
 # close file stream
 file_in.close()
 
def write_file(filename, filecontent):
 file_out = open(filename,'w') # create file
 file_out.write(filename) 
 file_out.write(filecontent)
 file_out.close()
 
main()

cygwin+python3下报错:UnicodeDecodeError: 'gb2312' codec can't decode byte 0xac in position 25: illegal multibyte sequence

修改打开文件参数

file_in = open(file_source,'r',encoding='UTF-8')

修改为如下

# load file
# for each row
## if match
## output
 
def main():
 print ('hhh')
 file_source = 'listening_questions.txt'
 #target_dir = ''
 file_in = open(file_source,'r',encoding='UTF-8')
 template_str = 'ZTPO'
 
 outfilename = 'head' #first file before match target 
 output_content = ''
 while 1:
 line = file_in.readline()
 if not line:
 break
 
 if line.find(template_str) != -1:
 write_file(outfilename,output_content)
 outfilename = line.strip('\n')
 output_content = '' # clear content of output file
 else:
 output_content += line # append content 
 write_file(outfilename,output_content) #for the last file
 # close file stream
 file_in.close()
 
def write_file(filename, filecontent):
 outfilename = './'+filename+'.txt' # output file tile
 file_out = open(outfilename,'w',encoding='UTF-8') # create file
 file_out.write(filename) 
 file_out.write(filecontent)
 file_out.close()
 
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现自动登录人人网并访问最近来访者实例
Sep 26 Python
Python中的数学运算操作符使用进阶
Jun 20 Python
python中 chr unichr ord函数的实例详解
Aug 06 Python
python并发2之使用asyncio处理并发
Dec 21 Python
Python使用修饰器进行异常日志记录操作示例
Mar 19 Python
python3发送邮件需要经过代理服务器的示例代码
Jul 25 Python
python 数据提取及拆分的实现代码
Aug 26 Python
Cython编译python为so 代码加密示例
Dec 23 Python
Pyecharts绘制全球流向图的示例代码
Jan 08 Python
python 线性回归分析模型检验标准--拟合优度详解
Feb 24 Python
Jupyter加载文件的实现方法
Apr 14 Python
基于nexus3配置Python仓库过程详解
Jun 15 Python
在linux系统下安装python librtmp包的实现方法
Jul 22 #Python
django搭建项目配置环境和创建表过程详解
Jul 22 #Python
对python中基于tcp协议的通信(数据传输)实例讲解
Jul 22 #Python
Django使用中间键实现csrf认证详解
Jul 22 #Python
python Tcp协议发送和接收信息的例子
Jul 22 #Python
利用Python实现手机短信监控通知的方法
Jul 22 #Python
如何使用django的MTV开发模式返回一个网页
Jul 22 #Python
You might like
有关phpmailer的详细介绍及使用方法
2013/01/28 PHP
关于Zend Studio 配色方案插件的介绍
2013/06/24 PHP
php封装好的人民币数值转中文大写类
2015/12/20 PHP
php文件缓存方法总结
2016/03/16 PHP
PHP框架Laravel插件Pagination实现自定义分页
2020/04/22 PHP
Laravel中validation验证 返回中文提示 全局设置的方法
2019/09/29 PHP
不同编码的页面表单数据乱码问题解决方法
2015/02/15 Javascript
jquery实现简单合拢与展开网页面板的方法
2015/09/01 Javascript
实例代码详解javascript实现窗口抖动及qq窗口抖动
2016/01/04 Javascript
使用jQuery Ajax 请求webservice来实现更简练的Ajax
2016/08/04 Javascript
工作中常用的js、jquery自定义扩展函数代码片段汇总
2016/12/22 Javascript
node.js中grunt和gulp的区别详解
2017/07/17 Javascript
iview table render集成switch开关的实例
2018/03/14 Javascript
微信小程序自定义对话框弹出和隐藏动画
2018/07/19 Javascript
vue项目中使用Hbuilder打包app 设置沉浸式状态栏的方法
2018/10/22 Javascript
浅谈Angular 观察者模式理解
2018/11/01 Javascript
vue.js实现的幻灯片功能示例
2019/01/18 Javascript
vue+iview/elementUi实现城市多选
2019/03/28 Javascript
一篇文章,教你学会Vue CLI 插件开发
2019/04/17 Javascript
node.js中事件触发器events的使用方法实例分析
2019/11/23 Javascript
Python对象属性自动更新操作示例
2018/06/15 Python
python使用ddt过程中遇到的问题及解决方案【推荐】
2018/10/29 Python
Pycharm更换python解释器的方法
2018/10/29 Python
解决Pycharm下面出现No R interpreter defined的问题
2018/10/29 Python
python3爬虫怎样构建请求header
2018/12/23 Python
python 项目目录结构设置
2020/02/14 Python
详解python with 上下文管理器
2020/09/02 Python
python各种excel写入方式的速度对比
2020/11/10 Python
阿迪达斯芬兰官方网站:adidas芬兰
2017/01/30 全球购物
房地产销售员的自我评价分享
2013/12/04 职场文书
知名企业招聘广告词大全
2014/03/18 职场文书
幼儿园新年寄语
2014/04/03 职场文书
毕业证代领委托书
2014/09/26 职场文书
群众路线剖析材料怎么写
2014/10/09 职场文书
奖励申请报告范文
2015/05/15 职场文书
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏