python分割一个文本为多个文本的方法
作者:sdulmy 时间:2022-09-01 06:40:33
本文实例为大家分享了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()
来源:https://blog.csdn.net/sdulmy/article/details/89608941
标签:python,分割文本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
window.location 对象所包含的属性
2024-04-16 10:32:14
对Python获取屏幕截图的4种方法详解
2023-11-19 09:45:41
详解git commit --amend 用法
2023-03-21 00:29:52
![](https://img.aspxhome.com/file/2023/1/117541_0s.jpg)
解决pycharm运行程序出现卡住scanning files to index索引的问题
2023-02-14 05:43:52
潜谈产品设计中的可用性和可访问性
2009-01-18 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20091/18/prdocut_reg-60s.jpg)
PHP实现的AES双向加密解密功能示例【128位】
2023-09-30 14:52:51
如何让WML页面自己更新?
2008-05-21 13:35:00
python 实现 hive中类似 lateral view explode的功能示例
2021-08-20 13:51:45
![](https://img.aspxhome.com/file/2023/6/101236_0s.jpg)
ASP.NET MVC4入门教程(二):添加一个控制器
2024-05-11 09:26:25
![](https://img.aspxhome.com/file/2023/4/126204_0s.png)
使用Python来编写HTTP服务器的超级指南
2022-12-28 20:40:24
![](https://img.aspxhome.com/file/2023/2/90652_0s.png)
pycharm 使用tab跳出正在编辑的括号(){}{}等问题
2023-06-14 03:17:55
![](https://img.aspxhome.com/file/2023/0/60300_0s.jpg)
使用Python实现企业微信的自动打卡功能
2021-02-27 02:41:34
python打开url并按指定块读取网页内容的方法
2023-07-07 11:00:36
asp base64加解密函数代码
2011-03-31 11:02:00
insert select与select into 的用法使用说明
2012-01-05 18:47:58
深入讲解Python函数中参数的使用及默认参数的陷阱
2022-04-21 20:09:14
关于SSD目标检测模型的人脸口罩识别
2023-06-20 05:20:56
![](https://img.aspxhome.com/file/2023/6/130986_0s.jpg)
pygame实现雷电游戏雏形开发
2021-04-14 13:52:31
![](https://img.aspxhome.com/file/2023/6/93976_0s.jpg)
js鼠标滑过图片震动特效的方法
2023-08-23 21:38:50
Django2.1.7 查询数据返回json格式的实现
2023-09-28 09:54:20
![](https://img.aspxhome.com/file/2023/9/63239_0s.png)