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 相关文章推荐
利用打码兔和超人打码自封装的打码类分享
Mar 16 Python
用Python输出一个杨辉三角的例子
Jun 13 Python
Python实现的堆排序算法原理与用法实例分析
Nov 22 Python
Python数据分析库pandas基本操作方法
Apr 08 Python
基于Python开发chrome插件的方法分析
Jul 07 Python
利用python打开摄像头及颜色检测方法
Aug 03 Python
Django 项目重命名的实现步骤解析
Aug 14 Python
详解Django配置优化方法
Nov 18 Python
python实现简单井字棋游戏
Mar 04 Python
python用opencv 图像傅里叶变换
Jan 04 Python
Python可变集合和不可变集合的构造方法大全
Dec 06 Python
Python编写冷笑话生成器
Apr 20 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
文件上传程序的全部源码
2006/10/09 PHP
快速配置PHPMyAdmin方法
2008/06/05 PHP
防止MySQL注入或HTML表单滥用的PHP程序
2009/01/21 PHP
php设计模式 Decorator(装饰模式)
2011/06/26 PHP
PHP中余数、取余的妙用
2015/06/29 PHP
javascript比较文档位置
2008/04/08 Javascript
如何设置一定时间内只能发送一次请求
2014/02/28 Javascript
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
原生JS实现LOADING效果
2015/03/16 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
angularjs+bootstrap菜单的使用示例代码
2017/03/07 Javascript
Angular.JS去掉访问路径URL中的#号详解
2017/03/30 Javascript
vue2.0 自定义日期时间过滤器
2017/06/07 Javascript
js 事件的传播机制(实例讲解)
2017/07/20 Javascript
JS实现的全排列组合算法示例
2017/10/09 Javascript
利用CDN加速react webpack打包后的文件详解
2018/02/22 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
Node.js assert断言原理与用法分析
2019/01/04 Javascript
Js代码中的span拼接问题解决
2019/11/22 Javascript
以一段代码为实例快速入门Python2.7
2015/03/31 Python
CentOS中升级Python版本的方法详解
2017/07/10 Python
python文件特定行插入和替换实例详解
2017/07/12 Python
Python自定义函数定义,参数,调用代码解析
2017/12/27 Python
Python2中文处理纪要的实现方法
2018/03/10 Python
Anaconda入门使用总结
2018/04/05 Python
Python中使用Counter进行字典创建以及key数量统计的方法
2018/07/06 Python
python实现简单tftp(基于udp协议)
2018/07/30 Python
python 函数内部修改外部变量的方法
2018/12/18 Python
python中函数返回多个结果的实例方法
2020/12/16 Python
使用纯HTML5编写一款网页上的时钟的代码分享
2015/11/16 HTML / CSS
时装界的“朋克之母”:Vivienne Westwood
2017/07/06 全球购物
巴西化妆品商店:Lojas Rede
2019/07/26 全球购物
创业计划书的内容步骤和要领
2014/01/04 职场文书
运动会演讲稿200字
2014/08/25 职场文书
考试作弊检讨书怎么写?
2014/12/21 职场文书
python字符串的一些常见实用操作
2022/04/06 Python