Monday 15 August 2011

python - Remove user from a tweet -



python - Remove user from a tweet -

i have python script removes rt i.r retweet tweet, # hash tag.. want remove username i.e, tweet has @userxyz: i.e. @ symbol followed username followed :(colon).. want remove such users: illustration if tweet "@bugun: mhp’li kemallettin yılmaz bank asya'ya yapılan intihardır http://t.co/akxmgmuuss @bugun http://t.co/fyjbr098tw}" want remove @bugun: , result "mhp’li kemallettin yılmaz bank asya'ya yapılan intihardır http://t.co/akxmgmuuss @bugun http://t.co/fyjbr098tw"

here code hash , rt removal tweet:

# coding:utf-8 import sys, re x = open("test.txt", "r") line in x: z = lambda line: re.compile('\#').sub('', re.compile('rt @').sub('@', line, count=1).strip()) print z(line) savefile = open("test_result.txt", "a") savefile.write(z(line)) savefile.write("\n") savefile.close()

using sample gave:

"@bugun: mhp’li kemallettin yılmaz bank asya'ya yapılan intihardır http://t.co/akxmgmuuss @bugun http://t.co/fyjbr098tw}"

the code (python 3.4):

# coding:utf-8 import sys, re x = open("test.txt", "r") line in x: z = lambda line: re.compile('\#').sub('', re.compile('^@\w+: ').sub('', line, count=1).strip()) print (z(line)) savefile = open("test_result.txt", "a") savefile.write(z(line)) savefile.write("\n") savefile.close()

will output:

mhp’li kemallettin yılmaz bank asya'ya yapılan intihardır http://t.co/akxmgmuuss @bugun http://t.co/fyjbr098tw

python regex

No comments:

Post a Comment