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 相关文章推荐
pycharm 使用心得(三)Hello world!
Jun 05 Python
Python的Flask框架及Nginx实现静态文件访问限制功能
Jun 27 Python
python中的单引号双引号区别知识点总结
Jun 23 Python
Python使用sklearn库实现的各种分类算法简单应用小结
Jul 04 Python
python+rsync精确同步指定格式文件
Aug 29 Python
Python 实现自动导入缺失的库
Oct 29 Python
python Manager 之dict KeyError问题的解决
Dec 21 Python
春节到了 教你使用python来抢票回家
Jan 06 Python
利用python下载scihub成文献为PDF操作
Jul 09 Python
python简单实现9宫格图片实例
Sep 03 Python
python 如何引入协程和原理分析
Nov 30 Python
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
Apr 06 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
1.PHP简介
2006/10/09 PHP
PHP检测用户语言的方法
2015/06/15 PHP
详解php与ethereum客户端交互
2018/04/28 PHP
php处理多图上传压缩代码功能
2018/06/13 PHP
基于jQuery实现的百度导航li拖放排列效果,即时更新数据库
2012/07/31 Javascript
深入理解jQuery中live与bind方法的区别
2013/12/18 Javascript
为指定的元素添加遮罩层的示例代码
2014/01/15 Javascript
Ext修改GridPanel数据和字体颜色、css属性等
2014/06/13 Javascript
jquery中append()与appendto()用法分析
2014/11/14 Javascript
js改变Iframe中Src的方法
2015/05/05 Javascript
延时加载JavaScript代码提高速度
2015/12/27 Javascript
JavaScript类的写法
2016/09/17 Javascript
AngularJs $parse、$eval和$observe、$watch详解
2016/09/21 Javascript
jQuery ajax的功能实现方法详解
2017/01/06 Javascript
layui中layer前端组件实现图片显示功能的方法分析
2017/10/13 Javascript
Angular实现的日程表功能【可添加及隐藏显示内容】
2017/12/27 Javascript
jQuery实现基本隐藏与显示效果的方法详解
2018/09/05 jQuery
Vue实现简单分页器
2018/12/29 Javascript
详解Next.js页面渲染的优化方案
2019/01/27 Javascript
Python的Django框架中的URL配置与松耦合
2015/07/15 Python
ubuntu环境下python虚拟环境的安装过程
2018/01/07 Python
Python实现的根据文件名查找数据文件功能示例
2018/05/02 Python
python实现机器人卡牌
2019/10/06 Python
基于python修改srt字幕的时间轴
2020/02/03 Python
Python改变对象的字符串显示的方法
2020/08/01 Python
Python自动化之UnitTest框架实战记录
2020/09/08 Python
详解如何使用CSS3中的结构伪类选择器和伪元素选择器
2020/01/06 HTML / CSS
英国人最爱的饰品网站:Accessorize
2016/08/22 全球购物
个人简历中的自我评价范例
2013/10/29 职场文书
孝老爱亲模范事迹
2014/01/24 职场文书
高考寄语大全
2014/04/08 职场文书
中学生期中自我鉴定
2014/04/20 职场文书
《大禹治水》教学反思
2014/04/27 职场文书
文明礼貌演讲稿
2014/05/12 职场文书
2019新员工心得体会
2019/06/25 职场文书
Python中的 enumerate和zip详情
2022/05/30 Python