简体   繁体   English

循环搜索结果,使用python查找字符串

[英]loop through results looking for a string using python

i have a csv and i'd like to find every occurrence of 'http://'. 我有一个csv,我想找到所有出现的“ http://”。 using the code below i get nothing. 使用下面的代码我什么也没得到。 at this point, i'd like to know why i'm not getting any results at all (not even the 'hi' is bring printed). 在这一点上,我想知道为什么我没有得到任何结果(甚至连“ hi”都没有印出来)。 i'm a noob and it's just not making sense to me. 我是菜鸟,对我来说这没有意义。 explanations are welcome, links are fine but only if they help me understand... 欢迎解释,链接很好,但前提是它们可以帮助我理解...

import csv

with open('a_bunch_of_urls_and_other_stuff.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')

    # remove results that dont have 'http://'     
    for result in reader:
        # print result      # this prints everything from the cvs
        # print result[2]   # this prints out the column with urls

        if result[2] == 'http://':
           print 'hi'

I am assuming you trying to check if result[2] contains 'http://'. 我假设您尝试检查result [2]是否包含“ http://”。
You can try: 你可以试试:

if "http://" in result[2]:
    print result[2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM