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下函数参数的传递(参数带星号的说明)
Sep 19 Python
python中模块查找的原理与方法详解
Aug 11 Python
spark dataframe 将一列展开,把该列所有值都变成新列的方法
Jan 29 Python
获取Pytorch中间某一层权重或者特征的例子
Aug 17 Python
Python中itertools的用法详解
Feb 07 Python
Pycharm连接gitlab实现过程图解
Sep 01 Python
关于python tushare Tkinter构建的简单股票可视化查询系统(Beta v0.13)
Oct 19 Python
如何基于Python爬虫爬取美团酒店信息
Nov 03 Python
Python对excel的基本操作方法
Feb 18 Python
python实现Thrift服务端的方法
Apr 20 Python
关于python中readlines函数的参数hint的相关知识总结
Jun 24 Python
Python Flask实现进度条
May 11 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
一些星际专用术语解释
2020/03/04 星际争霸
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
2013/05/07 PHP
php几个预定义变量$_SERVER用法小结
2014/11/07 PHP
PHP关联数组实现根据元素值删除元素的方法
2015/06/26 PHP
如何将php数组或者对象传递给javascript
2014/03/20 Javascript
json属性名为什么要双引号(个人猜测)
2014/07/31 Javascript
如何动态加载外部Javascript文件
2015/12/02 Javascript
vuejs在解析时出现闪烁的原因及防止闪烁的方法
2016/09/19 Javascript
Vue2.0表单校验组件vee-validate的使用详解
2017/05/02 Javascript
JavaScript-定时器0~9抽奖系统详解(代码)
2017/08/16 Javascript
js实现本地时间同步功能
2017/08/26 Javascript
Element-ui tree组件自定义节点使用方法代码详解
2018/09/17 Javascript
判断iOS、Android以及PC端的示例代码
2018/11/15 Javascript
vue父子组件的通信方法(实例详解)
2019/11/10 Javascript
javascript中layim之查找好友查找群组
2021/02/06 Javascript
50行代码实现贪吃蛇(具体思路及代码)
2013/04/27 Python
Python学习笔记(一)(基础入门之环境搭建)
2014/06/05 Python
Python3使用requests登录人人影视网站的方法
2016/05/11 Python
Python+django实现简单的文件上传
2016/08/17 Python
Python实现自定义函数的5种常见形式分析
2018/06/16 Python
python将秒数转化为时间格式的实例
2018/09/16 Python
python制作简单五子棋游戏
2019/06/18 Python
python 实现视频 图像帧提取
2019/12/10 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
Merchant 1948澳大利亚:新西兰领先的鞋类和靴子供应商
2018/03/24 全球购物
英国时尚优质的女装:Hope Fashion
2018/08/14 全球购物
Structs界面控制层技术
2013/10/11 面试题
医药个人求职信范文
2014/01/29 职场文书
小区推广策划方案
2014/06/06 职场文书
小学教育见习报告
2014/10/31 职场文书
活动经费申请报告
2015/05/15 职场文书
六一活动主持词
2015/06/30 职场文书
2016年感恩教师节活动总结
2016/04/01 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
详解Javascript实践中的命令模式
2021/05/05 Javascript
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL