0

I'm trying to learn Python and currently doing some exercises online. One of them involves reading zip files.

When I do:

import zipfilezp=zipfile.ZipFile('MyZip.zip')print(zp.read('MyText.txt'))

it prints:

b'Hello World'

I just want a string with "Hello World". I know it's stupid, but the only way I could think of was to do:

import rere.match("b'(.*)'",zp.read('MyText.txt'))

How am I supposed to do it?

askedOct 2, 2011 at 22:02
mowwwalker's user avatar
3
  • @John, it makes it "b'Hello World'"CommentedOct 2, 2011 at 22:09
  • I'm dumbfounded that this didn't get flagged as a possible duplicate and closed in seconds.CommentedOct 2, 2011 at 22:11
  • 1
    Given that I sometimes feel that Python is growing too complex, and has grown too many conflicting ways of doing the exact same thing over the years, I am terribly pleased that we three produced textually the exact same answer to this question independently of each other. :)CommentedOct 2, 2011 at 22:20

3 Answers3

7

You need to decode the raw bytes in the string into real characters. Try running.decode('utf-8') on the value you are getting back fromzp.read() before printing it.

answeredOct 2, 2011 at 22:05
Brandon Rhodes's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It seems all three of you just about tied for the answer, but you got it first.
6

You need todecode the bytes to text first.

print(zp.read('MyText.txt').decode('utf-8'))
answeredOct 2, 2011 at 22:06
Ignacio Vazquez-Abrams's user avatar

Comments

6

Justdecode the bytes:

print(zp.read('MyText.txt').decode('UTF-8'))
answeredOct 2, 2011 at 22:06
phihag's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.