Python Forum
How to convert a Status object to String?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert a Status object to String?
#1
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.
Reply
#2
s = s[s.index('@mytwitter')+11:]
Reply
#3
(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'"
Reply
#4
You can try print(dir(s)) to see if s has a method converting to a string.
Reply
#5
(Jan-14-2018, 08:24 AM)Gribouillis Wrote: You can try print(dir(s)) to see if s has a method converting to a string.

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?
Reply
#6
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.
Reply
#7
text will return the text of the tweet
_json will retun json object with all tweet related info
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert address and broadcast to network object Skaperen 9 1,723 Mar-09-2023, 06:55 PM
Last Post: Skaperen
  convert string to float in list jacklee26 6 1,814 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,905 Dec-17-2022, 12:24 AM
Last Post: snippsat
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,229 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,274 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,167 Jun-16-2021, 02:11 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020