您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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中怎么將一個文本分割為多個文本就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。