python分割一個文本為多個文本的方法
本文實(shí)例為大家分享了python分割一個文本為多個文本,供大家參考,具體內(nèi)容如下
# 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
修改打開文件參數(shù)
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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張
- Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
- python實(shí)現(xiàn)任意位置文件分割的實(shí)例
- Python分割指定頁數(shù)的pdf文件方法
- Python實(shí)現(xiàn)模擬分割大文件及多線程處理的方法
- Python 逐行分割大txt文件的方法
- python與php實(shí)現(xiàn)分割文件代碼
- python簡單分割文件的方法
- Python實(shí)現(xiàn)分割文件及合并文件的方法
- python分割文件的常用方法
- 用python分割TXT文件成4K的TXT文件
- python實(shí)現(xiàn)大文本文件分割
相關(guān)文章
Python實(shí)戰(zhàn)項(xiàng)目之MySQL tkinter pyinstaller實(shí)現(xiàn)學(xué)生管理系統(tǒng)
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用MySQL、tkinter、 pyinstaller實(shí)現(xiàn)一個學(xué)生管理系統(tǒng),大家可以通過案例查缺補(bǔ)漏,提升水平2021-10-10python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用
這篇文章主要介紹了python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用的相關(guān)資料,需要的朋友可以參考下2021-05-05LyScript實(shí)現(xiàn)計算片段Hash并寫出Excel的示例代碼
本案例將學(xué)習(xí)運(yùn)用LyScript計算特定程序中特定某些片段的Hash特征值,并通過xlsxwriter這個第三方模塊將計算到的hash值存儲成一個excel表格,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-09-09提升Python Scrapy庫數(shù)據(jù)采集速度實(shí)現(xiàn)高效爬蟲
Scrapy是一個強(qiáng)大而靈活的Python爬蟲框架,被廣泛用于數(shù)據(jù)采集、網(wǎng)站抓取和網(wǎng)絡(luò)爬蟲開發(fā),本文將深入介紹Scrapy的功能和用法,并提供豐富的示例代碼,幫助更好地理解和應(yīng)用2023-11-11Python實(shí)現(xiàn)簡單拆分PDF文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡單拆分PDF文件的方法,可實(shí)現(xiàn)將一個PDF文件拆分成指定份數(shù)的功能,涉及pyPdf模塊的使用技巧,需要的朋友可以參考下2015-07-07