簡體   English   中英

如何獲取python腳本以讀取配置文件中的所有值?

[英]How can I get my python script to read all values from a config file?

我有幾個配置文件。 一個帶有一些團隊名稱,另一個帶有職位。 我的每個用戶都將上傳一個包含團隊和職位的文件。 團隊和職位必須與配置文件中的內容匹配,否則將顯示錯誤。 我可以讓腳本從每個配置文件中僅讀取一個值。 它不會讀取其余的值。 我如何獲取它讀取配置文件中的所有值?

以下是兩個配置文件:對於團隊:[團隊]團隊=巴塞羅那,拜仁,國際米蘭,切爾西

職位:

[Positions]

positions = striker, midfielder, defender, goalkeeper

這是一個示例文本文件:

Teams   Positions   User ID
Barcelona   goalkeeper  BCTG-F
Barcelona   striker BCTG-F
Bayern  defender    BCTG-F
Bayern  striker    BCTG-F
Inter   striker    BCTG-F
Inter   midfielder  BCTG-F
Chelsea midfielder  BCTG-F
Chelsea goalkeeper  BCTG-F

這是腳本:

#!usr/bin/python


from subprocess import *

import sys
import ConfigParser
import os
import csv
from sys import argv
script, user_id, team_file = argv



def main():

    #get the actions

    def teamCalls():
        actions = ConfigParser.SafeConfigParser()
        actions.read('/etc/nagios/ingestion/team.cfg')
        for section_name in actions.sections():
            for name, value in actions.items(section_name):

                return '%s' %(value)

    teamCalls()

   #get the object types

    def positionTypes():
        objects = ConfigParser.SafeConfigParser()
        objects.read('/etc/nagios/ingestion/position.cfg')
        for section_name in objects.sections():
            for name, value in objects.items(section_name):

                return '%s' % (value)

    positionTypes()

    # checking path to file and user id
    try:
            f = csv.reader(open(team_file, "rb"), delimiter='\t')
    except:  
            logging.error('No such file or directory. Please try again')
    else:
            for line in f: 
                for row in f:                                  
                    if user_id != row[2]:
                        print ("User ID is incorrect")                                                                                 
                    elif teamCalls() != row[0]:

                        print ("Wrong team")                                                                       
                    elif positionTypes() != row[1]:
                        print ("Position not valid")

                    else: 
                        print row

    finally:
        print "all error checks done!"






main()

sys.exit(0) 

您可以在循環的最內層語句中“返回”。 這將導致它立即返回,而不會循環遍歷其余值。 如果您想要一個值列表,則可以執行以下操作:

calls = []
for section_name in actions.sections():
    for name,value in actions.items(section_name):
       calls.append(str(value))  # same as '%s' % (value)
return calls

那個怎么樣?

對於open('team.cfg','r')中的行:list_fu = [line.split()中列的列] print(list_fu)

輸出:

['Teams', 'Positions', 'User', 'ID']
['Barcelona', 'goalkeeper', 'BCTG-F']
['Barcelona', 'striker', 'BCTG-F']
['Bayern', 'defender', 'BCTG-F']
['Bayern', 'striker', 'BCTG-F']
['Inter', 'striker', 'BCTG-F']
['Inter', 'midfielder', 'BCTG-F']
['Chelsea', 'midfielder', 'BCTG-F']
['Chelsea', 'goalkeeper', 'BCTG-F']

如果您想閱讀這些位置,現在應該在列表EG->位置列表中,因此該列為1。現在,如果條件滿足,您將捕獲“團隊”和“用戶”:

對於list_fu [1]中的行:如果line =='goalkeeper':print(line)#做您想做的事

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM