python output from a file like image, or npz file

69 Views Asked by At

I’ve tried to read a npz file and it turn out the output like below

b’PK\x04\x03\x14\x00\x00\x00\x07\x00\x05\x00!\x00M\xd05\xda\xa1\xb7\
x00\x00\x00\x07\x01\x00\x00\x06\x00\x14\x00PK.npy\x01\x00\x10\x00.

I know it is unreadable for human but I really want to know what is the meaning of every character like b, PK,\ and the number on it

what is the meaning of every character on those output? like the slash or number on it? is it just random or? please help me. Thank you

2

There are 2 best solutions below

0
Mark Setchell On

Notation like the following means it is a Python bytes type:

b'......'

The P and K are just printable characters, i.e. the regular letters P and K. If you man ascii you will see that P and K are 80 and 75 in decimal.

The rest are hex representations of bytes, so \x04 just means a byte with the value 4.

5
rellachs On

You can display your binary data as 0's and 1's like this

xx = b'PK\x04\x03\x14\x00\x00\x00\x07'

for x in xx:
     print("{:08b}".format(x))

Output:

01010000
01001011
00000100
00000011
00010100
00000000
00000000
00000000
00000111

However, looking at the data is not equivalent to understanding its meaning. It only becomes meaningful if you know the specifications of the file format.