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

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

相关文章

  • 使用Pytorch+PyG实现MLP的详细过程

    使用Pytorch+PyG实现MLP的详细过程

    图神经网络是最近 AI 领域最热门的方向之一,下面这篇文章主要给大家介绍了关于使用Pytorch+PyG实现MLP的详细过程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • 浅谈Python xlwings 读取Excel文件的正确姿势

    浅谈Python xlwings 读取Excel文件的正确姿势

    这篇文章主要介绍了浅谈Python xlwings 读取Excel文件的正确姿势,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • python 命令行参数模块argparse的实现

    python 命令行参数模块argparse的实现

    本文主要介绍了python 命令行参数模块argparse的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Python线程之定位与销毁的实现

    Python线程之定位与销毁的实现

    这篇文章主要介绍了Python线程之定位与销毁的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • python3.x如何向mysql存储图片并显示

    python3.x如何向mysql存储图片并显示

    这篇文章主要介绍了python3.x如何向mysql存储图片并显示问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Python编程技巧连接列表的八种操作方法

    Python编程技巧连接列表的八种操作方法

    这篇文章主要为大家介绍了Python编程技巧之连接列表的八种操作方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-10-10
  • Python自动登录QQ的实现示例

    Python自动登录QQ的实现示例

    这篇文章主要介绍了Python自动登录QQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python+matplotlib绘制不同大小和颜色散点图实例

    Python+matplotlib绘制不同大小和颜色散点图实例

    这篇文章主要介绍了Python+matplotlib绘制不同大小和颜色散点图实例,matplotlib的用法是比较多种多样的,下面一起看看其中的一个实例吧
    2018-01-01
  • Python中关于print和return的区别

    Python中关于print和return的区别

    这篇文章主要介绍了Python中关于print和return的区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python实现Linux的find命令实例分享

    Python实现Linux的find命令实例分享

    本文给大家分享的是使用python简单实现模拟linux的find命令的实例代码,推荐给大家,希望大家能够喜欢
    2017-06-06

最新评论