Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove usernames from twitter data using python

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?

like image 683
gauri garg Avatar asked Oct 28 '25 03:10

gauri garg


2 Answers

import re
Tweet = "Hello@username"
Tweet = re.sub('@[^\s]+','',Tweet)

This code will remove the @username and Hello will not be removed.

like image 182
Negi Babu Avatar answered Oct 29 '25 16:10

Negi Babu


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.

like image 45
Suraj Shetty Avatar answered Oct 29 '25 17:10

Suraj Shetty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!