我有一个python脚本,其中包含以下部分 -
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]
当我尝试运行此文件时,它会显示行
中有错误str_response = response2.read()
它说 -
str_response = response2.read() ^ IndentationError:意外缩进
我是python的新手,无法弄清楚这段代码的缩进是什么。任何人都知道我在这里做错了什么?
答案 0 :(得分:4)
我通过帖子编辑模式复制了你的代码,正如其他人所说,你有缩进的标签和空格。在Python 3中,您需要使用其中一个,但不能同时使用两者。
以下是原始代码,标签为[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]
以下是使用4个空格而不是制表符的相同代码:
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]