Python解决UnicodeDecodeError并分割文本文件
本文介绍在Python中处理文本文件时遇到的UnicodeDecodeError错误,通过指定encoding='UTF-8'解决编码问题,并提供一段示例代码实现按特定模板字符串将大文本分割为多个小文件的功能。
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也欢迎大家多多支持本站。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









