Python 缩进错误:应为缩进块

Python 缩进错误:应为缩进块,python,csv,Python,Csv,我不断收到这个错误,告诉我缩进我的块,但我不知道我需要在哪里这样做,特别是如果我运行的是two-try子句。我试图允许我的第二个try子句像第一个一样打印到日志中。以下是我到目前为止的情况: #!usr/bin/python from subprocess import * import sys import ConfigParser import os import csv import getopt import time import datetime from datetime im

我不断收到这个错误,告诉我缩进我的块,但我不知道我需要在哪里这样做,特别是如果我运行的是two-try子句。我试图允许我的第二个try子句像第一个一样打印到日志中。以下是我到目前为止的情况:

#!usr/bin/python


from subprocess import *
import sys
import ConfigParser
import os
import csv
import getopt
import time
import datetime
from datetime import date
from time import gmtime, strftime
import logging
from sys import argv
script, solution_id, input_file = argv

#creating time stamp and returning as a string to add to solution id log name
def timeIzNow():  
    full = time.strftime(" %Y-%m-%d %H:%M:%S")

    return full

#set up logging to file
LOG_FILENAME = solution_id  + timeIzNow() 
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s %(process)d',
                    datefmt='%d %b %Y %H:%M:%S', 
                    filename=LOG_FILENAME,
              filemode='w')   
# defining a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# setting a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# telling the handler to use this format
console.setFormatter(formatter)
# adding the handler to the root logger
logging.getLogger('').addHandler(console)

#set up configuration Parser
config = ConfigParser.RawConfigParser()
config.read('/etc/nagios/ingestion/objectItems.cfg')
config.read('/etc/nagios/ingestion/action.cfg')

#get objects
objects = config.get('Objects', 'objects')

#get actions
actions = config.get('Actions', 'actions')

#if no object is found, run error
assert(sys.argv[1] != None), "object does not exist"

#logging debug 
#logging.debug('object does not exist')

#Get inputs and check value and path to file


try:
f = csv.reader(open(input_file, "rb")) 
except:
    logging.error('No such file or directory. Please try again')   
    for line in f:

        try: 
            for row in f: 

                if solution_id != row[2]:
                    print "Solution ID is invalid. Pleae check the number and try again"
        except ValueError: 
            logging.error('Solution ID is invalid. Please check the number and try again') 
                else:
                    print row







finally: 
     print "all error checks done!"
什么是

else:
  print row

连接到?它应该从需要一个else的内容缩进,但它在第二个try语句的第1列…

。你有一个没有动作的if语句

    for row in f: 
        try:    
            if solution_id != row[2]:
        except ValueError: 
            logging.error('Solution ID is invalid. Please check the number and try again') 
需要有类似的东西

    for row in f: 
        try:    
            if solution_id != row[2]:
                print "row error"
        except ValueError: 
            logging.error('Solution ID is invalid. Please check the number and try again') 
if语句试图将except用作条件后的操作

编辑:: 我没有简历来测试。但我没有从这段代码中得到其他错误:

#!usr/bin/python


from subprocess import *
import sys
import ConfigParser
import os
import csv
import getopt
import time
import datetime
from datetime import date
from time import gmtime, strftime
import logging
from sys import argv
script, solution_id, input_file = argv

#creating time stamp and returning as a string to add to solution id log name
def timeIzNow():  
    full = time.strftime(" %Y-%m-%d %H:%M:%S")

    return full

#set up logging to file
LOG_FILENAME = solution_id  + timeIzNow() 
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s %(process)d',
                    datefmt='%d %b %Y %H:%M:%S', 
                    filename=LOG_FILENAME,
              filemode='w')   
# defining a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# setting a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# telling the handler to use this format
console.setFormatter(formatter)
# adding the handler to the root logger
logging.getLogger('').addHandler(console)

#set up configuration Parser
config = ConfigParser.RawConfigParser()
config.read('/etc/nagios/ingestion/objectItems.cfg')
config.read('/etc/nagios/ingestion/action.cfg')

#get objects
objects = config.get('Objects', 'objects')

#get actions
actions = config.get('Actions', 'actions')

#if no object is found, run error
assert(sys.argv[1] != None), "object does not exist"

#logging debug 
#logging.debug('object does not exist')

#Get inputs and check value and path to file


try:
    f = csv.reader(open(input_file, "rb")) 
except:
    logging.error('No such file or directory. Please try again')   
    for line in f:

        try: 
            for row in f: 

                if solution_id != row[2]:
                    print "Solution ID is invalid. Pleae check the number and try again"
        except ValueError: 
            logging.error('Solution ID is invalid. Please check the number and try again') 
        else:
            print row







finally: 
     print "all error checks done!"

如果
if solution\u id!=第[2]行:
是否为真?您似乎缺少一行。获取一个IDE,它会告诉您,如果作为命令参数的解决方案id与csv文件中的解决方案id不相等,则会弹出一个错误并为其装入一个日志文件。您给了我们83行代码,但没有提到哪一行有问题。Python会准确地告诉您哪一行有缩进问题,并且很容易看到,这是因为您有一个
if
语句,在它的正上方没有正文。这应该是注释,而不是回答它是
try/except/else
块的一部分,所以这不是问题。我修复了“else”问题,但我现在得到了一个无效语法,当前代码中“else”最后一个字母上的克拉在第一次尝试后需要缩进:f=cvs.read并将else移回与第二次尝试一致;除以下情况外: