简体   繁体   English

如何防止python打印二进制符号

[英]how to prevent python from printing the binary sign

I need to print the comments of files inside a zip file : 我需要在zip文件中打印文件的注释:

import zipfile
def info_zip(archive_name):
   with zipfile.ZipFile(archive_name) as challenge:
       for info in challenge.infolist():
            print(info.comment)

but the results i am get aren't pretty: 但是我得到的结果不是很好:

b'G'
b' '
b'E'
b' '
b' '
b'*'
b'*'
b' '
b'E'
  1. How to get rid from those b at the start of every comment? 如何在每次评论开始时摆脱那些b?
  2. Why are they like that, i know what they are mean buy isn't the comment's content is by the author of the zip file, why he would like to store them as binary ? 他们为什么会这样,我知道他们的意思是购买不是评论的内容是由zip文件的作者提供的,为什么他想将它们存储为二进制文件?

Python 3.x Python 3.x

The b indicates that it is a bytes -object that is printed. b表示它是打印的字节对象 To get from bytes to string you have to decode the bytes object ( Encode is transform a string to a bytes object). 要从字节转换为字符串,您必须解码 bytes对象( 编码是将字符串转换为bytes对象)。

In order to decode a bytes object you'' have to know the used character encoding . 为了解码字节对象,您必须知道所使用的字符编码

My guess is that the zip-file header is simply ASCII encoded. 我的猜测是zip文件头只是ASCII编码的。 You should thus be able to get the wanted result by calling: 因此,您应该能够通过调用以下命令获得所需结果:

myBytesObject.decode('ASCII')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM