python分割一个文本为多个文本的方法
作者:sdulmy 发布时间:2022-09-01 06:40:33
标签:python,分割文本
本文实例为大家分享了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


猜你喜欢
- 一、Pytest简介Pytest is a mature full-featured Python testing tool that he
- 本文为大家分享了mysql 5.7.16 免安装版本教程,供大家参考,具体内容如下MySQL: 5.7.16 程序目
- from keras.utils.np_utils import to_categorical注意:当使用categorical_cross
- @ResponseBody 和 @RequestBody 注解的区别1 前言在详述 @ResponseBody 和 @RequestBody
- 描述:输入一个大于0的整数n,输出1到n的全排列:例如:n=3,输出[[3, 2, 1], [2, 3, 1], [2, 1, 3], [3
- Numpy随机矩阵: np.random.randn(d0, d1, d2, ...)矩阵大小与形状: np.ndarray.size 与
- 问题:在论坛发表回复时出现“The table is full”的提示,字面意义上是数据表已满的意思。因为很少有开发者遭遇单一表超过4G的情
- 本文实例讲述了JavaScript观察者模式(publish/subscribe)原理与实现方法。分享给大家供大家参考,具体如下:观察者模式
- 对于python的tkinter库来说,如果需要弹出文件选择框,我们需要引入一下tkinter.filedialog包,让用户直观地先择一个
- 前言在python中, 切片是一个经常会使用到的语法, 不管是元组, 列表还是字符串, 一般语法就是:sequence[ilow:ihigh
- (1)在校大学生。最好是数学或计算机相关专业,编程能力还可以的话,稍微看一下爬虫知识,主要涉及一门语言的爬虫库、html解析、内容存储等,复
- 先来看看什么是书签查找: 当优化器所选择的非聚簇索引只包含查询请求的一部分字段时,就需要一个查找(lookup)来检索其他字段来满足请求。对
- window.close(); 关闭浏览器窗口js代码的总结介绍序号关闭代码需要确认无任何作用无需确认测试1window.close()IE
- 前言:MYSQL 应该是最流行了 WEB 后端数据库。虽然 NOSQL 最近越来越多的被提到,但是相信大部分架构师还是会选择 MYSQL 来
- 例子老规矩,先上一个代码:def add(s, x): return s + xdef gen(): for i in range(4):
- 一、问题描述在用python开发时经常用到logging这个包,根据官方示例,如果要指定日志级别可以写成如下的方式。import loggi
- 类的定义假如要定义一个类 Point,表示二维的坐标点:# point.pyclass Point: def __init__(
- 多线程编程当中, 线程的存在形态比较抽象. 通过前台线程\后台线程, 可以有效理解线程运行顺序.(复杂的多线程程序可以通过设置线程优先级实现
- 1 HSV上下限颜色的HSV上下限如下表:2 追踪单个颜色import cv2 as cvimport numpy as npcap = c
- 最多16列。 create table test ( f1 int, f2 int, f3 int, f4 int, f5 int, f6