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基础语法(Python基础知识点)
Feb 28 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
关于python2 csv写入空白行的问题
Jun 22 Python
Numpy中的mask的使用
Jul 21 Python
Python交互式图形编程的实现
Jul 25 Python
Golang GBK转UTF-8的例子
Aug 26 Python
python安装gdal的两种方法
Oct 29 Python
关于sys.stdout和print的区别详解
Dec 05 Python
Python3将ipa包中的文件按大小排序
Apr 17 Python
Python如何脚本过滤文件中的注释
May 27 Python
Python爬取酷狗MP3音频的步骤
Feb 26 Python
pytorch 权重weight 与 梯度grad 可视化操作
Jun 05 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 常用的header头部定义汇总
2015/06/19 PHP
PHP实现的支付宝支付功能示例
2019/03/26 PHP
tp5框架前台无限极导航菜单类实现方法分析
2020/03/29 PHP
JS运行耗时操作的延时显示方法
2010/11/19 Javascript
Jquery带搜索框的下拉菜单
2013/05/06 Javascript
js浮点数保留两位小数点示例代码(四舍五入)
2013/12/26 Javascript
在Firefox下js select标签点击无法弹出
2014/03/06 Javascript
firefox浏览器用jquery.uploadify插件上传时报HTTP 302错误
2015/03/01 Javascript
介绍JavaScript中Math.abs()方法的使用
2015/06/14 Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
2017/01/19 Javascript
vue数据双向绑定原理解析(get & set)
2017/03/08 Javascript
vue生成随机验证码的示例代码
2017/09/29 Javascript
layui-table表复选框勾选的所有行数据获取的例子
2019/09/13 Javascript
vue框架中props的typescript用法详解
2020/02/17 Javascript
js绘制一条直线并旋转45度
2020/08/21 Javascript
Javascript call及apply应用场景及实例
2020/08/26 Javascript
vue-cli3 热更新配置操作
2020/09/18 Javascript
详解Vue.js 可拖放文本框组件的使用
2021/03/03 Vue.js
详解K-means算法在Python中的实现
2017/12/05 Python
使用Python实现windows下的抓包与解析
2018/01/15 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
2019/03/18 Python
python实现桌面气泡提示功能
2019/07/29 Python
Tensorflow获取张量Tensor的具体维数实例
2020/01/19 Python
Python 之 Json序列化嵌套类方式
2020/02/27 Python
通过实例解析Python return运行原理
2020/03/04 Python
Django 自定义404 500等错误页面的实现
2020/03/08 Python
python如何查看安装了的模块
2020/06/23 Python
html5 input属性使用示例
2013/06/28 HTML / CSS
不打扫卫生检讨书
2014/02/12 职场文书
我的中国心演讲稿
2014/09/04 职场文书
优秀校长事迹材料
2014/12/24 职场文书
房地产财务经理岗位职责
2015/04/08 职场文书
让人瞬间清醒的句子,句句经典,字字如金
2019/07/08 职场文书
七年级之家长会发言稿范文
2019/09/04 职场文书
Java实战之用Swing实现通讯录管理系统
2021/06/13 Java/Android
简单聊聊Golang中defer预计算参数
2022/03/25 Golang