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的Tornado框架实现数据可视化的教程
May 02 Python
Python实现二叉堆
Feb 03 Python
python编码最佳实践之总结
Feb 14 Python
Python 循环语句之 while,for语句详解
Apr 23 Python
python Pandas 读取txt表格的实例
Apr 29 Python
python 获取页面表格数据存放到csv中的方法
Dec 26 Python
Python 确定多项式拟合/回归的阶数实例
Dec 29 Python
python hash每次调用结果不同的原因
Nov 21 Python
关于python中plt.hist参数的使用详解
Nov 28 Python
Python安装tar.gz格式文件方法详解
Jan 19 Python
如何基于windows实现python定时爬虫
May 01 Python
使用PyCharm官方中文语言包汉化PyCharm
Nov 18 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生成的html meta和link标记在body标签里 顶部有个空行
2010/05/18 PHP
destoon在360浏览器下出现用户被强行注销的解决方法
2014/06/26 PHP
Zend Framework生成验证码并实现验证码验证功能(附demo源码下载)
2016/03/22 PHP
PHP.vs.JAVA
2016/04/29 PHP
php_pdo 预处理语句详解
2016/11/21 PHP
网页和浏览器兼容性问题汇总(draft1)
2009/06/01 Javascript
Javascript中自动切换焦点实现代码
2012/12/15 Javascript
基于AGS JS开发自定义贴图图层
2017/03/31 Javascript
详解node中创建服务进程
2017/05/09 Javascript
JavaScript实现鼠标滚轮控制页面图片切换功能示例
2017/10/14 Javascript
Vue 表情包输入组件的实现代码
2019/01/21 Javascript
基于vue写一个全局Message组件的实现
2019/08/15 Javascript
js 实现碰撞检测的示例
2020/10/28 Javascript
[02:09:59]火猫TV国士无双dota2 6.82版本详解(下)
2014/09/29 DOTA
[47:20]DAC2018 4.4 淘汰赛 Optic vs Mineski 第一场
2018/04/05 DOTA
python实现的简单猜数字游戏
2015/04/04 Python
使用基于Python的Tornado框架的HTTP客户端的教程
2015/04/24 Python
Python list操作用法总结
2015/11/10 Python
Python 递归函数详解及实例
2016/12/27 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
2017/01/20 Python
Ubuntu安装Jupyter Notebook教程
2017/10/18 Python
对python append 与浅拷贝的实例讲解
2018/05/04 Python
Python实现统计给定字符串中重复模式最高子串功能示例
2018/05/16 Python
pycharm通过anaconda安装pyqt5的教程
2020/03/24 Python
如何用python 操作zookeeper
2020/12/28 Python
基于 HTML5 Canvas实现 的交互式地铁线路图
2018/03/05 HTML / CSS
税务干部鉴定材料
2014/02/11 职场文书
学生会部长竞聘书
2014/03/31 职场文书
教师党员自我剖析材料
2014/09/29 职场文书
幼儿园小班家长评语
2014/12/30 职场文书
建党伟业电影观后感
2015/06/01 职场文书
python实现自定义日志的具体方法
2021/05/28 Python
vue 实现上传组件
2021/05/31 Vue.js
简单介绍Python的第三方库yaml
2021/06/18 Python
Python 一键获取电脑浏览器的账号密码
2022/05/11 Python
MySQL提升大量数据查询效率的优化神器
2022/07/07 MySQL