How to convert a Status object to String? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to convert a Status object to String? (/thread-7518.html) |
How to convert a Status object to String? - Vucko - Jan-14-2018 Really need some help, been stuck on this for hours. So I'm working on a small Twitter bot that is using Tweepy. I have it working so that when my Twitter account gets mentioned in a Tweet I grab what the tweet says and run it through a function that will determine what my reply is. The big issue I have is that when I get a copy of the tweet that I was in, it includes my Twitter username which will make my reply not work. For example: "@mytwitteraccount their message" I only need the "their message" part of it. I thought the best way to remove my username from the output with there tweet was to do s = "@mytwitter their message" ' '.join(s.split()[1:])the problem I've been having is since the output isn't a String It won't work. I've tried str() no luck. Heres my code for what I'm doing to get the tweet. tweets = api.search(q="@myaccount",count=100,include_entities=True) t = ['@myaccount', '@MyAccount'] for s in tweets: sn = s.user.screen_name m = "@%s my reply" % (sn) s = api.update_status(m, s.id) for result in tweets: print (result.text)Any help would be great, I looked over the Tweepy Documentation and couldn't find anything. RE: How to convert a Status object to String? - Larz60+ - Jan-14-2018 s = s[s.index('@mytwitter')+11:] RE: How to convert a Status object to String? - Vucko - Jan-14-2018 (Jan-14-2018, 07:06 AM)Larz60+ Wrote:s = s[s.index('@mytwitter')+11:] Tried that but I'm getting "AttributeError: 'Status' object has no attribute 'index'" RE: How to convert a Status object to String? - Gribouillis - Jan-14-2018 You can try print(dir(s)) to see if s has a method converting to a string.
RE: How to convert a Status object to String? - Vucko - Jan-14-2018 (Jan-14-2018, 08:24 AM)Gribouillis Wrote: You can try Just tried that out and got this for the output, ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api', '_json', 'author', 'contributors', 'coordinates', 'created_at', 'destroy', 'entities', 'favorite', 'favorite_count', 'favorited', 'geo', 'id', 'id_str', 'in_reply_to_screen_name', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'is_quote_status', 'lang', 'parse', 'parse_list', 'place', 'retweet', 'retweet_count', 'retweeted', 'retweets', 'source', 'source_url', 'text', 'truncated', 'user']I notice there is __str__ would that be any help? If so how would I use it? RE: How to convert a Status object to String? - Gribouillis - Jan-14-2018 No __str__ is used in the expression str(s) , but you already tried that. I would try s.text() or s.text . You could also explore all the attributes that don't start with an underscore.
RE: How to convert a Status object to String? - buran - Jan-14-2018 text will return the text of the tweet_json will retun json object with all tweet related info
|