python解压bz2文件命令,在Python中解压缩.bz2文件

python解压bz2文件命令,在Python中解压缩.bz2文件So,thisisaseeminglysimplequestion,butI’mapparentlyveryverydull.Ihavealittlescriptthatdownloadsallthe.bz2filesfromawebpage,butforsomereasonthedecompressingofthatfile…

大家好,又见面了,我是你们的朋友全栈君。

python解压bz2文件命令,在Python中解压缩.bz2文件

So, this is a seemingly simple question, but I’m apparently very very dull. I have a little script that downloads all the .bz2 files from a webpage, but for some reason the decompressing of that file is giving me a MAJOR headache.

I’m quite a Python newbie, so the answer is probably quite obvious, please help me.

In this bit of the script, I already have the file, and I just want to read it out to a variable, then decompress that? Is that right? I’ve tried all sorts of way to do this, I usually get “ValueError: couldn’t find end of stream” error on the last line in this snippet. I’ve tried to open up the zipfile and write it out to a string in a zillion different ways. This is the latest.

openZip = open(zipFile, “r”)

s = ”

while True:

newLine = openZip.readline()

if(len(newLine)==0):

break

s+=newLine

print s

uncompressedData = bz2.decompress(s)

Hi Alex, I should’ve listed all the other methods I’ve tried, as I’ve tried the read() way.

METHOD A:

print ‘decompressing ‘ + filename

fileHandle = open(zipFile)

uncompressedData = ”

while True:

s = fileHandle.read(1024)

if not s:

break

print(‘RAW “%s”‘, s)

uncompressedData += bz2.decompress(s)

uncompressedData += bz2.flush()

newFile = open(steamTF2mapdir + filename.split(“.bz2″)[0],”w”)

newFile.write(uncompressedData)

newFile.close()

I get the error:

uncompressedData += bz2.decompress(s)

ValueError: couldn’t find end of stream

METHOD B

zipFile = steamTF2mapdir + filename

print ‘decompressing ‘ + filename

fileHandle = open(zipFile)

s = fileHandle.read()

uncompressedData = bz2.decompress(s)

Same error :

uncompressedData = bz2.decompress(s)

ValueError: couldn’t find end of stream

Thanks so much for you prompt reply. I’m really banging my head against the wall, feeling inordinately thick for not being able to decompress a simple .bz2 file.

By the by, used 7zip to decompress it manually, to make sure the file isn’t wonky or anything, and it decompresses fine.

解决方案

You’re opening and reading the compressed file as if it was a textfile made up of lines. DON’T! It’s NOT.

uncompressedData = bz2.BZ2File(zipFile).read()

seems to be closer to what you’re angling for.

Edit: the OP has shown a few more things he’s tried (though I don’t see any notes about having tried the best method — the one-liner I recommend above!) but they seem to all have one error in common, and I repeat the key bits from above:

opening … the compressed file as if

it was a textfile … It’s NOT.

open(filename) and even the more explicit open(filename, ‘r’) open, for reading, a text file — a compressed file is a binary file, so in order to read it correctly you must open it with open(filename, ‘rb’). ((my recommended bz2.BZ2File KNOWS it’s dealing with a compressed file, of course, so there’s no need to tell it anything more)).

In Python 2.*, on Unix-y systems (i.e. every system except Windows), you could get away with a sloppy use of open (but in Python 3.* you can’t, as text is Unicode, while binary is bytes — different types).

In Windows (and before then in DOS) it’s always been indispensable to distinguish, as Windows’ text files, for historical reason, are peculiar (use two bytes rather than one to end lines, and, at least in some cases, take a byte worth ‘\0x1A’ as meaning a logical end of file) and so the reading and writing low-level code must compensate.

So I suspect the OP is using Windows and is paying the price for not carefully using the ‘rb’ option (“read binary”) to the open built-in. (though bz2.BZ2File is still simpler, whatever platform you’re using!-).

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/138646.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • idea2022最新激活码 csdn【中文破解版】

    (idea2022最新激活码 csdn)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.htmlLGWSVFD4PZ-eyJsaWNlbnNlSW…

  • 计算机网络中广域网和局域网_广域网接入技术有哪些

    计算机网络中广域网和局域网_广域网接入技术有哪些 局域网与广域网   局域网(LocalAreaNetwork,LAN)是将分散在有限地理范围内的多台计算机通过传输媒体连接起来的通信网络,通过功能完善的网络软件,实现计算机之间的相互通信和资源共享;广域网(WideAreaNetwork,WAN)是在传输距离较长的前提下所发展的相关技术的集合,用于将大区域范围内的各种计算机设备和通信设备互联在一起,组成一个资源共享的通信网络。 …

    2022年10月10日
  • python+opencv图像模板匹配—多模板匹配

    python+opencv图像模板匹配—多模板匹配

  • protel99se中文版

    protel99se中文版教程:1、解压压缩包,打开“Protel99SE”文件夹,双击“setup.exe”开始安装软件。2、输入用户信息,可以任意输入,然后输入3、选择软件的安装位置,建议默认,便于。4、选择安装类型,选择typical典型的进行安装。5、选择开始菜单文件夹,默认即可。6、准备安装软件,点击next。7、正在安装,请稍等一下。8、安装完成,资源地址:protel99se中文版…

  • mac配置vscodec语言环境_vscodejava环境变量配置

    mac配置vscodec语言环境_vscodejava环境变量配置登陆终端输入java-version查看java的版本输入java-verbose查看java的安装环境路径没有的话,自己去搜一下怎么安装java的jdk,记得选1.18版本安装路径在这里安装完jdk后,输入/usr/libexec/java_home-V查看jdk版本复习什么是JDKJavaDevelopmentKit开发工具包包含Java开发工具和JREjava开发工具:编译工具javac.exe,jar.exe打包工具等。JRE=JVM+javaS

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号