Tweepy, a bytes-like object is required, not str. How do i fix this error?

Triple A

error: Traceback (most recent call last): File "C:\Users\zakar\PycharmProjects\Tweepy-bots\main.py", line 29, in c=c.replace("im ","") TypeError: a bytes-like object is required, not 'str'

error Error picture

this is how the code looks like:

`import tweepy
import tweepy as tt
import time
import sys
import importlib
importlib.reload(sys)




#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'

#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_query = "barca"
user = api.me()
print(user.name)

max_tweets = 100

for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
    c=tweet.text.encode('utf8')
    c=c.replace("im ","")
    answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm a bot!"
    print ("Reply:",answer)
    api.update_status(status=answer,in_reply_to_status_id=tweet.id)
    time.sleep(300) #every 5 minutes`
Kris

The problem is because you are encoding the text , and then replacing.

here

c=tweet.text.encode('utf8')
c=c.replace("im ","")

encode() will return bytes not a string. So in replace also you need to use the bytes. Like

c=tweet.text.encode('utf8')
c=c.replace(b"im ",b"")

Or you replace the content first and then encode.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix TypeError: a bytes-like object is required, not 'str'

"TypeError: a bytes-like object is required, not 'str'". How can I fix this?

Error: a bytes-like object is required, not 'str'

TypeError: a bytes-like object is required, not 'str' how do I tackle this?

Why is it giving me the error, "the JSON object must be str, not 'bytes'", and how do I fix it?

a bytes-like object is required, not 'str' error in python

TypeError: a bytes-like object is required, not 'str' error in Python 2.7

Python error: TypeError: a bytes-like object is required, not 'str'

Python gnupg "Type Error: a bytes-like object is required, not 'str'"

How to resolve Json Error: TypeError: a bytes-like object is required, not 'str'

How to solve the error: TypeError: a bytes-like object is required, not 'str' with FLASK-MAIL?

How can I remove 'TypeError: a bytes-like object is required, not 'str''

a bytes-like object is required not 'str'

TypeError: a bytes-like object is required, not 'str', but I have a bytes object

Flask Unit Testing and not understanding my fix for "TypeError: a bytes-like object is required, not 'str'"

Count the duplicate rows in excel using python and i am getting error TypeError: a bytes-like object is required, not 'str'

TypeError: a bytes-like object is required, not 'str' but type shows bytes

a bytes-like object is required, not 'str' JSON File opened as STR

a bytes-like object is required, not 'str' despite i provided byte object

bacnet_scan output a csv file error TypeError: a bytes-like object is required, not 'str'

Using Python 3.5, throwing error: TypeError: a bytes-like object is required, not 'str'

Python sockets error TypeError: a bytes-like object is required, not 'str' with send function

URL open , decode encode error TypeError: a bytes-like object is required, not 'str'

Error: A bytes-like object is required, not 'list'

TypeError: a bytes-like object is required, not 'str' when I'm inputting command in console

How can I fix error: 'expected str, bytes or os.PathLike object, not _io.TextIOWrapper' in Python FTP

TypeError: a bytes-like object is required, not 'str' WSGI server

TypeError: a bytes-like object is required, not 'str' 'xxx';'xxx'

Python 3 - TypeError: a bytes-like object is required, not 'str'