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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python实现模拟分割大文件及多线程处理的方法

    本文实例讲述了Python实现模拟分割大文件及多线程处理的方法.分享给大家供大家参考,具体如下: #!/usr/bin/env python #--*-- coding:utf-8 --*-- from random import randint from time import ctime from time import sleep import queue import threading class MyTask(object): """具体的任务类"&qu

  • python分割文件的常用方法

    本文大家整理了一些比较好用的关于python分割文件的方法,方法非常的简单实用.分享给大家供大家参考.具体如下: 例子1 指定分割文件大小 配置文件 config.ini: 复制代码 代码如下: [global] #原文件存放目录 dir1=F:\work\python\3595\pyserver\test #新文件存放目录 dir2=F:\work\python\3595\pyserver\test1 python 代码如下: 复制代码 代码如下: #!/usr/bin/python # -*

  • Python中使用pypdf2合并、分割、加密pdf文件的代码详解

    朋友需要对一个pdf文件进行分割,在网上查了查发现这个pypdf2可以完成这些操作,所以就研究了下这个库,并做一些记录.首先pypdf2是python3版本的,在之前的2版本有一个对应pypdf库. 可以使用pip直接安装: pip install pypdf2 官方文档: pythonhosted.org/PyPDF2/ 里面主要有这几个类: PdfFileReader . 该类主要提供了对pdf文件的读操作,其构造方法为: PdfFileReader(stream, strict=True,

  • 用python分割TXT文件成4K的TXT文件

    复制代码 代码如下: ########################## # # # 为了避免截断中文字符 # # 文件要求是 unicode 编码 # # txt文件另存为对话框下面有下拉框,可选存 # # 储编码格式 # # # ########################## import os import struct filename = str(raw_input("Please enter an old file name: ")) filenamepre = s

  • python实现将文件夹内的每张图片批量分割成多张

    一.说在前面 需求:有一张长为960,宽为96的图片,需要将其分割成10张96*96的图片并存放在另外一个文件夹下,通过手工分割耗时且不规范,选择python写一个简单的程序完成. 二.源码 # -*- coding: utf-8 -*- """ Created on Thu Aug 23 18:19:09 2018 @author: Administrator """ import os from PIL import Image # 切割图片

  • python实现大文本文件分割

    本文实例为大家分享了python实现大文本文件分割的具体代码,供大家参考,具体内容如下 开发环境 Python 2 实现效果 通过文件拖拽或文件路径输入,实现自定义大文本文件分割. 代码实现 #coding:gbk import os,sys,shutil is_file_exits=False while not is_file_exits: files_list=[] if(len(sys.argv)==1): print('请输入要切割的文件完整路径:') files_path=raw_i

  • Python 逐行分割大txt文件的方法

    代码如下所示: # -*- coding: <encoding name> -*- import io LIMIT = 150000 file_count = 0 url_list = [] with io.open('D:\DB_NEW_bak\DB_NEW_20171009_bak.sql','r',encoding='utf-16') as f: for line in f: url_list.append(line) if len(url_list) < LIMIT: conti

  • Python实现分割文件及合并文件的方法

    本文实例讲述了Python实现分割文件及合并文件的方法.分享给大家供大家参考.具体如下: 分割文件split.py如下: #!/usr/bin/python ########################################################################## # split a file into a set of parts; join.py puts them back together; # this is a customizable ve

  • python实现任意位置文件分割的实例

    应用场景 在嵌入式开发中,常常需要将一个binary文件分割成多个文件,或者将一个binary的某块区域抓成一个单独文件.本篇blog以python为例,实现了以上需求; 实现代码 #!/usr/bin/python """ ./file_split.py, just for testing; """ import sys import os import re from os.path import join from array import

  • python简单分割文件的方法

    本文实例讲述了python简单分割文件的方法.分享给大家供大家参考.具体如下: 有的网站在上传文件时对文件大小有限制,因此可以将大文件分割成多个小文件再上传. #!/usr/bin/env python def split(filename, size): fp = open(filename, 'rb') i = 0 n = 0 temp = open(filename+'.part'+str(i),'wb') buf = fp.read(1024) while(True): temp.wri

  • Python分割指定页数的pdf文件方法

    如下所示: from PyPDF2 import PdfFileWriter, PdfFileReader # 开始页 start_page = 0 # 截止页 end_page = 5 output = PdfFileWriter() pdf_file = PdfFileReader(open("3.pdf", "rb")) pdf_pages_len = pdf_file.getNumPages() # 保存input.pdf中的1-5页到output.pdf

  • python与php实现分割文件代码

    前两天有个朋友说,想实现一个文本文件按照固定行数进行分割成多个文本文件,却不知如何实现.如果数据量小手动分割下就好了,如果数据量很大的话手动完成实在太耗费人力了,也不现实.那么就需要借助脚本去实现.既然有朋友想简单的完成这个任务,那么不如记录下来,给需要的朋友提供方便. 下面我就分别使用python和php进行脚本的实现和操作,当然用其他语言都能实现,大家可根据对语言的熟悉程度进行自主选择,如果有朋友还没有达到编写代码的能力的话,那么最起码对语言环境的使用要会,只要达到这些,就可以完成如下工作.

随机推荐