1

I have a python script which contains the following part-

import csv
import mechanize

"""
some code here
"""

with open('data2.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        response2 = browser.open(surl+"0"+row[0])
        str_response = response2.read()
        if "bh{background-color:" in str_response :
            print "Not Found"
        else :

            print "Found " + row[0]
            s_index=str_response.find("fref=search")+13
            e_index=str_response.find("</a><div class=\"bm bn\">")
            print str_response[s_index:e_index]

When I'm trying to run this file, it shows there's an error in the line

str_response = response2.read()

It says-

str_response = response2.read() ^ IndentationError: unexpected indent

I'm newbie in python and can not figure out what is the right indentation for this piece of code. Anyone have any idea what I'm doing wrong here?

Abdullah Shahriar
  • 355
  • 1
  • 3
  • 15

1 Answers1

4

I copied your code via the post edit mode, and as other have said, you have mixed tabs and spaces for your indentation. In Python 3, you need to use one or the other, but not both.

Here is your original code with the tabs marked as [TB]

import csv
import mechanize

"""
some code here
"""

with open('data2.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        response2 = browser.open(surl+"0"+row[0])
[TB][TB]str_response = response2.read()
[TB][TB]if "bh{background-color:" in str_response :
[TB][TB][TB]print "Not Found"
[TB][TB]else :

[TB][TB][TB]print "Found " + row[0]
[TB][TB][TB]s_index=str_response.find("fref=search")+13
[TB][TB][TB]e_index=str_response.find("</a><div class=\"bm bn\">")
[TB][TB][TB]print str_response[s_index:e_index]

and here is the same code using 4 spaces instead of tabs:

import csv
import mechanize

"""
some code here
"""

with open('data2.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        response2 = browser.open(surl+"0"+row[0])
        str_response = response2.read()
        if "bh{background-color:" in str_response :
            print "Not Found"
        else :

            print "Found " + row[0]
            s_index=str_response.find("fref=search")+13
            e_index=str_response.find("</a><div class=\"bm bn\">")
            print str_response[s_index:e_index]
James
  • 32,991
  • 4
  • 47
  • 70