0

I save below code in 1 file. lets say read_zipfile.py.

with open("demo.zip", "rb") as f:
 read_data = f.read()
 print (read_data) 

python2 giving below output:

[harsha@os]$ python2 read_zipfile.py
PK�flNdemo/PK
�flN����demo/hello.txtThi is Hello file
PK
�KK
 demo/hi.txtPK?�flN$��Ademo/
 �m@Q���^;T����m@Q���PK?
�flN����$ ���#demo/hello.txt
 �m@Q����m@Q����m@Q���PK?
�KK
 $ ���ademo/hi.txt
 ���B�������,���PK�

python3 giving below output:

[harsha@os]$ python3 read_zipfile.py
 b'PK\x03\x04\x14\x03\x00\x00\x00\x00\x88flN\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00demo/PK\x03\x04\n\x03\x00\x00\x00\x00\x88flN\x83\x9d\xd9\xc9\x12\x00\x00\x00\x12\x00\x00\x00\x0e\x00\x00\x00demo/hello.txtThi is Hello file\nPK\x03\x04\n\x03\x00\x00\x00\x00\xf0\x18KK\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00demo/hi.txtPK\x01\x02?\x03\x14\x03\x00\x00\x00\x00\x88flN\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00$\x00\x00\x00\x00\x00\x00\x00\x10\x80\xedA\x00\x00\x00\x00demo/\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x80m@Q\xa4\xd8\xd4\x01\x00^;T\xa4\xd8\xd4\x01\x80m@Q\xa4\xd8\xd4\x01PK\x01\x02?\x03\n\x03\x00\x00\x00\x00\x88flN\x83\x9d\xd9\xc9\x12\x00\x00\x00\x12\x00\x00\x00\x0e\x00$\x00\x00\x00\x00\x00\x00\x00 \x80\xa4\x81#\x00\x00\x00demo/hello.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x80m@Q\xa4\xd8\xd4\x01\x80m@Q\xa4\xd8\xd4\x01\x80m@Q\xa4\xd8\xd4\x01PK\x01\x02?\x03\n\x03\x00\x00\x00\x00\xf0\x18KK\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00$\x00\x00\x00\x00\x00\x00\x00 \x80\xa4\x81a\x00\x00\x00demo/hi.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x00\xb6\x96\xfa\x0fB\xd3\x01\x80\xd0\xd6\x16\xa4\xd8\xd4\x01\x00,\x17\x0f\xa4\xd8\xd4\x01PK\x05\x06\x00\x00\x00\x00\x03\x00\x03\x00\x14\x01\x00\x00\x8a\x00\x00\x00\x00\x00'

How can I get python2 output format using python3?

asked Mar 12, 2019 at 7:34
6
  • That is exactly the same data, it's just printed differently. What's your goal here? Why are you printing the binary representation of a zip file? Commented Mar 12, 2019 at 7:40
  • @Aran-Fey I want data to be print like python2 in python3 Commented Mar 12, 2019 at 7:41
  • @Aran-Fey : Can I convert python3 output in python2 format? Commented Mar 12, 2019 at 7:42
  • @Aran-Fey Python3 giving me in byte format. I want python2 output using python3. Commented Mar 12, 2019 at 7:49
  • Harsha Biyani: Why are you printing the binary data in a .zip file anyway? Commented Mar 12, 2019 at 8:10

2 Answers 2

3

In python3, f.read() returns a bytes, you should choose a encoding such as utf-8 and convert it to str.

Then it will be print like python2.

with open("demo.zip", "rb") as f:
 read_data = f.read()
 #print (read_data) 
 s = read_data.decode('latin1')
 print(s)
Harsha Biyani
7,28810 gold badges42 silver badges64 bronze badges
answered Mar 12, 2019 at 7:45
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this. This not giving me in the same format like Python2 output
it depends on , what is the actually decode your console use, it maybe not utf-8
@martineau : Thanks a lo for your help. latin1 helped me to solve the problem. Could you please add the answer? so that I can accept the same.
@Harsha Biyani: That's not necessary. I suggest you accept this one which contains the essence of the answer.
@HarshaBiyani: BTW, using the latin1 encoding to store binary data is kind of a "trick" in Python 3. See this answer to a related related question.
|
0

In order to dump raw bytes to STDOUT in Python 3, use the binary stream underlying sys.stdout. It's accessible as the .buffer attribute.

Change print(read_data) to

sys.stdout.buffer.write(read_data)
sys.stdout.buffer.write(b'\n')

The last line is necessary if you want to exactly mimic what print does.

answered Mar 12, 2019 at 9:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.