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实现实例
Apr 26 Python
Python和GO语言实现的消息摘要算法示例
Mar 10 Python
python 爬虫出现403禁止访问错误详解
Mar 11 Python
windows下python之mysqldb模块安装方法
Sep 07 Python
Python中整数的缓存机制讲解
Feb 16 Python
Python计算两个矩形重合面积代码实例
Sep 16 Python
python实现的多任务版udp聊天器功能案例
Nov 13 Python
使用pyhon绘图比较两个手机屏幕大小(实例代码)
Jan 03 Python
关于python 的legend图例,参数使用说明
Apr 17 Python
python判断是空的实例分享
Jul 06 Python
python使用matplotlib绘制折线图的示例代码
Sep 22 Python
超详细Python解释器新手安装教程
May 10 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中用加号与用array_merge合并数组的区别深入分析
2013/06/03 PHP
PHP APC的安装与使用详解
2013/06/13 PHP
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
2014/05/07 PHP
PHP开发微信支付的代码分享
2014/05/25 PHP
Yii模型操作之criteria查找数据库的方法
2016/07/15 PHP
详解php中curl返回false的解决办法
2019/03/18 PHP
tp5.1 框架数据库常见操作详解【添加、删除、更新、查询】
2020/05/26 PHP
jquery 学习之二 属性(类)
2010/11/25 Javascript
关于COOKIE个数与大小的问题
2011/01/17 Javascript
JS保存和删除cookie操作 判断cookie是否存在
2013/11/13 Javascript
基于jquery固定于顶部的导航响应浏览器滚动条事件
2014/11/02 Javascript
JavaScript将数字转换成大写中文的方法
2015/03/23 Javascript
学习JavaScript正则表达式
2015/11/13 Javascript
D3.js实现雷达图的方法详解
2016/09/22 Javascript
AngularJS页面带参跳转及参数解析操作示例
2017/06/28 Javascript
使用Node.js实现简易MVC框架的方法
2017/08/07 Javascript
vue实现城市列表选择功能
2018/07/16 Javascript
vue项目中实现的微信分享功能示例
2019/01/21 Javascript
如何使用CSS3+JQuery实现悬浮墙式菜单
2019/06/18 jQuery
vue+ElementUI 关闭对话框清空验证,清除form表单的操作
2020/08/06 Javascript
[22:20]初生之犊-TI4第5名LGD战队纪录片
2014/08/13 DOTA
Python 正则表达式操作指南
2009/05/04 Python
Python删除指定目录下过期文件的2个脚本分享
2014/04/10 Python
用Python实现QQ游戏大家来找茬辅助工具
2014/09/14 Python
Python获取当前函数名称方法实例分享
2018/01/18 Python
Python使用pandas对数据进行差分运算的方法
2018/12/22 Python
python网络编程:socketserver的基本使用方法实例分析
2020/04/09 Python
python中常见错误及解决方法
2020/06/21 Python
matplotlib实现数据实时刷新的示例代码
2021/01/05 Python
印尼美容产品购物网站:PerfectBeauty.id
2017/12/01 全球购物
开工庆典邀请函范文
2014/01/16 职场文书
幼儿园中班新学期寄语
2014/01/18 职场文书
行政专员的岗位职责
2014/03/10 职场文书
退休党员个人对照检查材料思想汇报
2014/09/29 职场文书
2014年行风建设工作总结
2014/12/01 职场文书
2015年学校工作总结范文
2015/04/20 职场文书