Reputation: 31
tweets = api.search(q="@mytwitter",count=100,include_entities=True)
t = ['@mytwitter', '@MyTwitter']
for s in tweets:
sn = s.user.screen_name
m = "@%s message" % (sn)
s = api.update_status(m, s.id)
#print(dir(s))
for result in tweets:
print str(result.text)
for userName in result.text:
if userName == "@mytwitter":
result.text.remove(userName)
print (userName)
else:
print("No username")
Not sure why userName isn't removing my Twitter name from result.text. Am I missing something? The output I'm getting is "No username"
repeated a bunch of times.
If it helps the orginal output for result.text is "@mytwitter their message"
I'm trying to remove the @mytwitter.
Upvotes: 1
Views: 210
Reputation: 418
Your problem is that the condition
if userName == "@mytwitter":
Is never true, so .remove() never called. If you want to search for your name inside the result.text, try this:
tweets = api.search(q="@mytwitter",count=100,include_entities=True)
for s in tweets:
sn = s.user.screen_name
m = "@%s message" % (sn)
s = api.update_status(m, s.id)
for tweeet in tweets:
if "@mytwitter" in tweeet.text:
tweeet.text = tweeet.text.replace("@mytwitter", "")
print(tweeet.text)
else:
print("No username")
p.s. The str object doesn't have .remove() method, try .replace() with empty string instead, as listed above.
Upvotes: 1
Reputation: 3384
Strings are immutable in Python so you have to assign your new string to another variable:
for tweet_text in tweets:
if "@mytwitter" in tweet_text:
new_str = tweet_text.replace("@mytwitter", "") # assigning new string wothout "@mytwitter" to another variable
print(new_str)
else:
print("No username")
Example:
tweets = ["@mytwitter","1234","0987@mytwitter","@mytwitter145236"," @mytwitter12"]
for tweet_text in tweets:
if "@mytwitter" in tweet_text:
new_str = tweet_text.replace("@mytwitter", "") # assigning new string wothout "@mytwitter" to another variable
print(new_str)
else:
print("No username")
Output:
No username
0987
145236
12
Upvotes: 0