Python从列表中过滤/删除URL [英] Python filter/remove URLs from a list
问题描述
我有一个约14000的URL文本文件.下面是几个示例:
I have a text file of URLs, about 14000. Below is a couple of examples:
http://www.domainname.com/pagename?CONTENT_ITEM_ID=100&; param2 = 123
http://www.domainname.com/images?IMAGE_ID=10
http://www.domainname.com/pagename?CONTENT_ITEM_ID=101¶m2=123
http://www.domainname.com/images?IMAGE_ID=11
http://www.domainname.com/pagename?CONTENT_ITEM_ID=102¶m2=123
http://www.domainname.com/pagename?CONTENT_ITEM_ID=100¶m2=123
http://www.domainname.com/images?IMAGE_ID=10
http://www.domainname.com/pagename?CONTENT_ITEM_ID=101¶m2=123
http://www.domainname.com/images?IMAGE_ID=11
http://www.domainname.com/pagename?CONTENT_ITEM_ID=102¶m2=123
我已将文本文件加载到Python列表中,并且试图将所有具有CONTENT_ITEM_ID的URL分离到它们自己的列表中.用Python做到这一点的最佳方法是什么?
I have loaded the text file into a Python list and I am trying to get all the URLs with CONTENT_ITEM_ID separated off into a list of their own. What would be the best way to do this in Python?
欢呼
推荐答案
这是Graeme的另一种替代方法,它使用了较新的列表理解语法:
Here's another alternative to Graeme's, using the newer list comprehension syntax:
list2= [line for line in file if 'CONTENT_ITEM_ID' in line]
您所喜欢的只是品味问题!
Which you prefer is a matter of taste!
这篇关于Python从列表中过滤/删除URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!