如何使for循环不复制数据?

2024-04-16 22:41:30 发布

您现在位置: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!"

Tags: 文件nameurlaliasactionallhgfile
1条回答
网友
1楼 · 发布于 2024-04-16 22:41:30

你的问题是线路:

    for line in current_file:        
        for row in current_file:  

对于Python解释器来说,这两个词的意思是一样的:从文件中读取行。Python对变量一无所知,它只是每行读取一次所有的行。相反,只需删除这些行中的第一行:

    for row in line:  

相关问题 更多 >