1

i have a csv and i'd like to find every occurrence of '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). 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'
3
  • 2
    It looks like you want to use if "http://" in result[2]. E.g. use search_term in result[2] not search_term == result[2]. You might also want to consider using a regexp if you think in the future you might also care about https:// or other protocols too. Commented Apr 16, 2015 at 22:25
  • 2
    Ya gotta give us something to go on! What does print result[2] look like for some of the rows? Should the test be if result[2].lower().startswith('http://'):? Commented Apr 16, 2015 at 22:28
  • basically, i want to "check" to see if there's a 'http://' in the row. if so, then print...if not then don't print. Commented Apr 16, 2015 at 22:48

1 Answer 1

1

I am assuming you trying to check if result[2] contains 'http://'.
You can try:

if "http://" in result[2]:
    print result[2]
Sign up to request clarification or add additional context in comments.

1 Comment

if result[2] == 'http://': checks if the contents of result[2] is exactly 'http://'. Where as my answer checks if result[2] has at least one sub-string with the value 'http://'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.