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操作MySQL数据库的方法分享
May 29 Python
利用一个简单的例子窥探CPython内核的运行机制
Mar 30 Python
动感网页相册 python编写简单文件夹内图片浏览工具
Aug 17 Python
Django实现的自定义访问日志模块示例
Jun 23 Python
Python实现字典(dict)的迭代操作示例
Jun 05 Python
深入了解Python枚举类型的相关知识
Jul 09 Python
Django框架基础模板标签与filter使用方法详解
Jul 23 Python
Python + Flask 实现简单的验证码系统
Oct 01 Python
python正则表达式的懒惰匹配和贪婪匹配说明
Jul 13 Python
python时间time模块处理大全
Oct 25 Python
python反扒机制的5种解决方法
Feb 06 Python
python 单机五子棋对战游戏
Apr 28 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
php常用的安全过滤函数集锦
2014/10/09 PHP
基于PHP技术开发客服工单系统
2016/01/06 PHP
Laravel框架路由设置与使用示例
2018/06/12 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
baidu博客的编辑友情链接的新的层窗口!经典~支持【FF】
2007/02/09 Javascript
js拦截alert对话框另类应用
2013/01/16 Javascript
基于jQuery选择器的整理集合
2013/04/26 Javascript
javascript 原型链维护和继承详解
2014/11/26 Javascript
JQuery报错Uncaught TypeError: Illegal invocation的处理方法
2015/03/13 Javascript
js实现的tab标签切换效果代码分享
2015/08/25 Javascript
javascript表单事件处理方法详解
2016/05/15 Javascript
JS前端加密算法示例
2016/12/22 Javascript
angular双向绑定模拟探索
2016/12/26 Javascript
JavaScript实现图片切换效果
2017/08/12 Javascript
bootstrap table实现合并单元格效果
2018/12/24 Javascript
jQuery Ajax async=>false异步改为同步时,解决导致浏览器假死的问题
2019/07/22 jQuery
vue实现页面切换滑动效果
2020/06/29 Javascript
Openlayers显示地理位置坐标的方法
2020/09/28 Javascript
在Python的Flask中使用WTForms表单框架的基础教程
2016/06/07 Python
Python的collections模块中namedtuple结构使用示例
2016/07/07 Python
基于Python fminunc 的替代方法
2020/02/29 Python
浅谈python 调用open()打开文件时路径出错的原因
2020/06/05 Python
python如何安装下载后的模块
2020/07/03 Python
HTML5的表单(绝对特别强大的功能)使用示例
2013/06/20 HTML / CSS
GLAMGLOW香港官网:明星出镜前的秘密武器
2017/03/16 全球购物
一套带答案的C++笔试题
2014/01/10 面试题
现金会计岗位职责
2013/12/05 职场文书
药店促销活动总结
2014/07/10 职场文书
2014法院干警廉洁警示教育思想汇报
2014/09/13 职场文书
护士工作失误检讨书
2014/09/14 职场文书
北京导游词
2015/02/12 职场文书
农村结婚典礼主持词
2015/06/29 职场文书
房产遗嘱范本
2015/08/06 职场文书
神州牡丹园的导游词
2019/11/20 职场文书
解决Python中的modf()函数取小数部分不准确问题
2021/05/28 Python