0

I'm learning CS61a at the beginning. When I open a text of Shakespeare's work, I use text.read().split(), after that all the elements are returned with the letter "b" at the beginning.

So why there is a "b" at the beginning? What's the meaning of it? Is there a way to fix this? Thanks in advance!

Here's the code:

    >>> shakes=urlopen('http://composingprograms.com/shakespeare.txt')
    >>> text=shakes.read().split()
    >>> text[:25]
    [b'A', b"MIDSUMMER-NIGHT'S", b'DREAM', b'Now', b',', b'fair', b'Hippolyta', b',', b'our', b'nuptial', b'hour', b'Draws', b'on', b'apace', b':', b'four', b'happy', b'days', b'bring', b'in', b'Another', b'moon', b';', b'but', b'O']
hungnguyen
  • 64
  • 1
  • 10

1 Answers1

0

Answer is here.

https://stackoverflow.com/a/6269785/8313460

To quote the Python 2.x documentation:

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

The Python 3.3 documentation states:

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

Willian Vieira
  • 646
  • 3
  • 9