如何让我的for循环不要复制我的数据?

时间:2015-01-07 23:00:49

标签: python

我是python的新手,我正在尝试让我的脚本读取文件中的正确区域。该脚本应该读取该文件,然后使用信息csv文件创建一个配置文件,该文件与所有要添加,更新或删除的参数一起上载。现在我只是注意到我的循环没有通过文件,它只读取最后一行。所以我移动了被调用的函数,我注意到它循环并多次打印出数据。当它只打算打印出来一次。以下是脚本正确运行时正常配置文件的样子:

define hostgroup {
     hostgroup_name                 ISD-CR-All_Servers_group      
     notes                                                        
     alias                                                        
     action_url                                                   
     notes_url                                                    
     members                                                      
     hostgroup_members                                            
}

以下是编写脚本的方式:

define hostgroup {
     hostgroup_name                 ISD-CR-All_Servers            
     notes                                                        
     alias                                                        
     action_url                                                   
     notes_url                                                    
     members                                                      
     hostgroup_members                                            
}

# Configuration file /etc/nagios/objects/solution1/ISD-CR-All_Servers.cfg
# Edited by PyNag on Wed Jan  7 22:47:21 2015

define hostgroup {
     hostgroup_name                 ISD-CR-All_Servers            
     notes                                                        
     alias                                                        
     action_url                                                   
     notes_url                                                    
     members                                                      
     hostgroup_members                                            
}

# Configuration file /etc/nagios/objects/solution1/ISD-CR-All_Servers.cfg
# Edited by PyNag on Wed Jan  7 22:47:22 2015

define hostgroup {
     hostgroup_name                 ISD-CR-All_Servers            
     notes                                                        
     alias                                                        
     action_url                                                   
     notes_url                                                    
     members                                                      
     hostgroup_members                                            
}

上传的文件如下所示:

Add Host Group  ISD-CR  ISD-CR-All_Servers      

Add Host Group  ISD-CR  ISD-CR-All_Servers      
Update  Service ISD-CR      ISD-CR-All_Servers  ISD-CR-Linux_Server
Update  Service ISD-CR      ISD-CR-Db_Servers   ISD-CR-Mango_db

Delete  Service Group   ISD-CR          
Delete  Service Group   ISD-CR          
Delete  Host Group  ISD-CR  ISD-CR-All_Servers      
Add Service Group   ISD-CR  ISD-CR-All_Servers_group        
Update  Service ISD-CR      ISD-CR-Web_Servers  ISD-CR-Web_link

这是我的剧本:

from pynag import Model
from pynag.Parsers import config
from subprocess import *
import subprocess
import sys

def addHostGroup():


            # Add hostgroup object
            hg = Model.Hostgroup()

            hg.set_filename('/etc/nagios/objects/solution1/{0}.cfg'.format(hostgroup_name))

            # Adding all attributes to allow any to be added if needed
            hg.hostgroup_name = row[3]
            hg.alias = row[4]
            hg.members = row[5]
            hg.hostgroup_members = row[6]
            hg.notes =  row[7]
            hg.notes_url = row[8]
            hg.action_url = row[9]

            # Save
            hg.save()

            print "hostgroup added"

def existHostGroup():        

        hostgroup = os.system('pynag list where hostgroup_name=hostgroup_name')
        if not hostgroup:
            logging.error("Hostgroup %s not found." % hostgroup_name)
            print 'adding objects'
            addHostGroup()


        print hostgroup
try:
        current_file = csv.reader(open(input_file, "rb"), delimiter='\t')
    except:  
        logging.error('No such file or directory. Please try again')
    else:
        for line in current_file:        
            for row in current_file:                        
                hostgroup_name = row[3]               
                if solution_id != row[2]:
                        logging.error('Solution ID is invalid. Please check the number and try again') 
                elif addAction() != row[0] and updateAction() != row[0] and deleteAction() != row[0]:
                        logging.error("Undefined action")
                elif object_hostgroup() != row[1] and object_service() != row[1] and object_servicegroup() != row[1]:
                        logging.error("Undefined object") 
                     # add in options checking if object(alias,hostgroup_name, etc) exists
                     # select add, modify or delete                                                                                                                    
                else:  
                    print row 
                    existHostGroup()


    finally:
        print "all error checks done!"

1 个答案:

答案 0 :(得分:0)

你的问题就在于:

    for line in current_file:        
        for row in current_file:  

对于Python解释器,这两者都意味着相同的事情:从文件中读取行。 Python对变量一无所知,它只是每行读取一次所有行。相反,只需删除第一行:

    for row in line: