python删除http hyperlin

2024-06-16 09:11:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从我的推特删除超链接 例如: 我曾说过:“改革医疗保健服务于病人,而不是企业医疗 http://t.co/WMKJKU4hl7@PNHP#单一付款人星期日http://t.co/i4bNrruUNS” 但我想得到的是:“改革医疗保健服务病人,而不是企业医疗” 怎么可能呢? 谢谢你!在

根据这些建议,我重写了我所有tweet列表的代码。我写了一个for循环,但没用:

我试过这个,因为我有一个tweets列表,但是没有为

for i in range(len(cleandata)): finaldata=[] if 'http://' in cleandata[i]: post = cleandata[i] position = post.index('http://') finaldata.append(post[:position]) else: finaldata.append(cleandata[i]) cleandata是我的tweet列表 你们知道吗?在


Tags: inhttp列表forpositionpost企业tweet
3条回答

您可以找到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]

结果

^{pr2}$

哦,很小的错误。。。我纠正了。。。谢谢各位!在

'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)

相关问题 更多 >