2013-12-17 48 views
0

我有一个文本,我想从中删除URL,但我有问题。如何从Python中的字符串中删除一些URL

document = re.sub('[^a-z]|http:\/\/\w+.\w+\/\w*', ' ', document) 

IGOT: 文件= 'RT @prettycolleges:凤凰城大学http://t.co/d5wxsy332r好'

>> 'rt prettycolleges university of phoenix http  t co d wxsy r good' 

,但我想这样的结果:rt prettycolleges university of phoenix good

什么我应该怎么做?

回答

0

你可以使用像

一个正则表达式'\ S * HTTP://.* \ s?(查找有一个URL字符串 - HTTP:// - 它与空白结束)

,且因为子功能取代你在找什么,代码应该是:

import re 
document = 'rt @prettycolleges: university of phoenix http://t.co/d5wxsy332r good' 

print re.sub(r'http:\\*/\\*/.*?\s', ' ', document) ## note the r (raw string) 
>> 'rt @prettycolleges: university of phoenix good' 
+0

它只是为了我所提到的例子中工作,但新的例子实在不行。 document ='rt @beasiswaindo:http:\/\/t.co \/uio40rq8hc beasiswa full s2 w的大学' – user3092781

+0

那是因为url不是以http://开头的(你已经跳过了斜杠)。我会编辑我的答案,所以也可以在这种情况下工作 – azuax

相关问题