Python读取分割压缩TXT Python读取分割压缩TXT文本文件实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python读取分割压缩TXT Python读取分割压缩TXT文本文件实例

子觉   2021-04-21 我要评论

废话不多说,上代码看吧!

'''
 
为了避免截断中文字符 
文件要求是 unicode 编码 
txt文件另存为对话框下面有下拉框,可选存 
储编码格式 
 
'''
 
import os 
import struct 
#导入所需python模块方法
 
filename = str(raw_input("Please enter an old file name: ")) 
filenamepre = str(raw_input("Please enter an new file name prefix: ")) 
count = 0
filecount = 0
maxcount = 20
newfilename = repr(filecount) + '.txt'
oldfile = open(filename,'rb') 
 
#www.iplaypy.com
bFirst = True
 
while True: 
  s = oldfile.read(512*8 - 4) 
if not s: 
  exit() 
 
filecount = filecount + 1
newfilename = filenamepre + repr(filecount).zfill(2) + '.txt'
newfile = open(newfilename,'wb') 
 
if not bFirst: 
  be = 0XFEFF
  newfile.write(struct.pack('H',be)) 
  newfile.write(s) 
 
be = 0X000A000D
newfile.write(struct.pack('I',be)) 
newfile.close() 
bFirst = False
oldfile.close() 
 
#文件相关操作切记要关闭

以上这篇Python读取分割压缩TXT文本文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们