Kwame Boame Kwame Boame - 2 months ago 18
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?


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