亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python分割一個文本為多個文本的方法

 更新時間:2019年07月22日 10:06:33   作者:sdulmy  
這篇文章主要為大家詳細介紹了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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python實戰(zhàn)項目之MySQL tkinter pyinstaller實現學生管理系統

    Python實戰(zhàn)項目之MySQL tkinter pyinstaller實現學生管理系統

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用MySQL、tkinter、 pyinstaller實現一個學生管理系統,大家可以通過案例查缺補漏,提升水平
    2021-10-10
  • Python圖像處理之圖像的灰度線性變換

    Python圖像處理之圖像的灰度線性變換

    這篇文章主要介紹了Python圖像處理之圖像的灰度線性變換,文章圍繞主題展開詳細的內容戒殺,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • python3使用迭代生成器實現減少內存占用

    python3使用迭代生成器實現減少內存占用

    這篇文章主要介紹了python3使用迭代生成器實現減少內存占用的相關資料,需要的朋友可以參考下
    2021-05-05
  • Python 中星號(*)的用法小結

    Python 中星號(*)的用法小結

    星號??*?? 往往被稱為乘法運算符,是所有程序中最為常用的運算符號之一,在Python 中,星號還有很多隱藏的強大功能, 本文將用最容易理解的例子來解釋星號*的 五個使用場景,從初級用法到高階用法,感興趣的朋友可以參考下
    2023-08-08
  • LyScript實現計算片段Hash并寫出Excel的示例代碼

    LyScript實現計算片段Hash并寫出Excel的示例代碼

    本案例將學習運用LyScript計算特定程序中特定某些片段的Hash特征值,并通過xlsxwriter這個第三方模塊將計算到的hash值存儲成一個excel表格,感興趣的可以跟隨小編一起學習一下
    2022-09-09
  • 提升Python Scrapy庫數據采集速度實現高效爬蟲

    提升Python Scrapy庫數據采集速度實現高效爬蟲

    Scrapy是一個強大而靈活的Python爬蟲框架,被廣泛用于數據采集、網站抓取和網絡爬蟲開發(fā),本文將深入介紹Scrapy的功能和用法,并提供豐富的示例代碼,幫助更好地理解和應用
    2023-11-11
  • Python實現簡單拆分PDF文件的方法

    Python實現簡單拆分PDF文件的方法

    這篇文章主要介紹了Python實現簡單拆分PDF文件的方法,可實現將一個PDF文件拆分成指定份數的功能,涉及pyPdf模塊的使用技巧,需要的朋友可以參考下
    2015-07-07
  • Python3爬蟲關于代理池的維護詳解

    Python3爬蟲關于代理池的維護詳解

    在本篇文章里小編給大家分享了關于Python3爬蟲關于代理池的維護詳解內容,需要的朋友們可以參考下。
    2020-07-07
  • Python中的with...as用法介紹

    Python中的with...as用法介紹

    這篇文章主要介紹了Python中的with...as用法介紹,本文直接給出用法實例,需要的朋友可以參考下
    2015-05-05
  • jupyter lab文件導出/下載方式

    jupyter lab文件導出/下載方式

    這篇文章主要介紹了jupyter lab文件導出/下載方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論