python删除http超链接
我想从我的推文中删除超链接 例如: 我曾经说过:“改革医疗服务,为病人服务,而不是企业医疗。” @PNHP#单一付款人星期日” 但我想得到:“改革医疗服务,为患者服务,而不是企业医疗” 怎么可能呢? 谢谢你 根据这些建议,我为我的推文列表重写了代码。我编写了一个for循环,但不起作用: 我试过这个,因为我有一个tweet列表,但没有为 范围内i的python删除http超链接,python,http,loops,for-loop,Python,Http,Loops,For Loop,我想从我的推文中删除超链接 例如: 我曾经说过:“改革医疗服务,为病人服务,而不是企业医疗。” @PNHP#单一付款人星期日” 但我想得到:“改革医疗服务,为患者服务,而不是企业医疗” 怎么可能呢? 谢谢你 根据这些建议,我为我的推文列表重写了代码。我编写了一个for循环,但不起作用: 我试过这个,因为我有一个tweet列表,但没有为 范围内i的(len(cleandata)): 最终数据=[] 如果cleandata[i]中的“http://”: post=cleandata[i] posit
(len(cleandata)):
最终数据=[]
如果cleandata[i]中的“http://”:
post=cleandata[i]
position=post.index('http://')
finaldata.append(post[:position])
其他:
finaldata.append(cleandata[i])
cleandata是我的推文列表
你们知道吗?你们可以找到http://所在的位置,然后切片
post = "Reform health care to serve patients, not corporate medicine http://t.co/WMKJKU4hl7 @PNHP #SinglePayerSunday http://t.co/i4bNrruUNS"
position = post.index('http://')
print post[:position]
结果
Reform health care to serve patients, not corporate medicine
哦,很小的错误。。。我纠正了它。。。谢谢大家
'finaldata=[]
for i in range(len(cleandata)):
if 'http://' in cleandata[i]:
post = cleandata[i]
position = post.index('http://')
finaldata.append(post[:position])
else:
finaldata.append(cleandata[i])
您可以尝试使用
re
import re
post = "Reform health care to serve patients, not corporate medicine http://t.co/WMKJKU4hl7 @PNHP #SinglePayerSunday http://t.co/i4bNrruUNS"
print re.sub(r'http://.*','',post)
第一次出现
http://
之后,是否要删除所有内容?这非常聪明!非常感谢你!事实上,现在我有另一个问题,我试着把它放进一个for循环,因为我确实有一个tweet列表。我试着把它放进一个for循环,但没有成功。我在上面公布了我的当前代码。。很抱歉,我不能让它工作