Kwame Boame Kwame Boame - 1 year ago 61
Python Question

Python-Twitter: Can't covert 'bytes' object to string

I am trying to encode tweets from unicode to utf-8 but I get the following error gets logged on CLI when I execute the file:

File "", line 21, in analyze
text += s.text.encode('utf-8')
TypeError: Can't convert 'bytes' object to str implicitly

Here is my code:

text = ""
for s in statuses:
if (s.lang =='en'):
text += s.text.encode('utf-8')

And here are the packages I am importing:

import sys
import operator
import requests
import json
import twitter
from watson_developer_cloud import PersonalityInsightsV2 as PersonalityInsights

How can I get the strings (tweet text) to be converted to the right encosing properly so I can use them? What am I doing wrong?

Answer Source

You should initialize your text as bytes by appending a leading b:

text = b"" 

This will allow the new bytes object to be concatenated without errors to the existing bytes object text

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download