I have fetched some data from Twitter using python. now I want to pre process it. how can I remove usernames if the tweet has username between two words and there is no space among them? I want to keep the words and only delete the username
for eg. text file: hello @rahulcan you help me? yes @tanyatell me?
output i want: hello can you help me? yes tell me?
import re
Tweet = "Hello@username"
Tweet = re.sub('@[^\s]+','',Tweet)
This code will remove the @username and Hello will not be removed.
import re
Tweet = "Hello@username"
Tweet = re.sub('@[\w]+','',Tweet)
Building on @NegiBabu's solution, Twitter only allows alphanumeric handles and so [\w] works as a better regex for this task. For e.g. with my proposed regex you wouldn't allow for @app#le to be matched.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With