Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I had some Python 2 code as follows (pardon the indentation):

def getZacksRating(symbol):

c = httplib.HTTPSConnection("www.zacks.com")

c.request("GET", "/stock/quote/"+symbol)

response = c.getresponse()

data = response.read()

ratingPart = data.split('<p class="rank_view">')[1]

result = ratingPart.partition("<span")[0].strip()

return result

print getZacksRating("AAPL")

So I changed it to (adding the b' ').

import http

def getZacksRating(symbol):

    c = http.client.HTTPSConnection("www.zacks.com")

    c.request("GET", "/stock/quote/"+symbol)

    response = c.getresponse()

    data = response.read()

    ratingPart = data.split(b'<p class="rank_view">')[1]

    result = ratingPart.partition(b"<span")[0].strip()

    return result

print(getZacksRating('AAPL'))

But it getting printed as below:

print(getZacksRating('AAPL'))

b'Strong Buy'

I don't wish to see the b' ' in my output. Just want to see strong Buy being printed. 

1 Answer

0 votes
by (36.8k points)

Its very simple, Instead of return result, use return result.decode('utf-8')

If you are a beginner and want to know more about Python the do check out the data science with python course

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...