[英]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'
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.