简体   繁体   English

如何获取python脚本以读取配置文件中的所有值?

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

I have a couple of config files. 我有几个配置文件。 One with some team names and the other with positions. 一个带有一些团队名称,另一个带有职位。 Each one of my users will upload a file with teams and positions on them. 我的每个用户都将上传一个包含团队和职位的文件。 The teams and positions must match what's in the config files otherwise an error will print. 团队和职位必须与配置文件中的内容匹配,否则将显示错误。 I can get the script to read only one value from each config file. 我可以让脚本从每个配置文件中仅读取一个值。 It doesn't read the rest of the values. 它不会读取其余的值。 How can I get it to read all of the values in the config files? 我如何获取它读取配置文件中的所有值?

Here are the two config files: for teams: [Teams] teams = Barcelona, Bayern, Inter, Chelsea 以下是两个配置文件:对于团队:[团队]团队=巴塞罗那,拜仁,国际米兰,切尔西

for positions: 职位:

[Positions]

positions = striker, midfielder, defender, goalkeeper

Here is a sample text file: 这是一个示例文本文件:

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

Here is the script: 这是脚本:

#!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) 

You have "return" on the inner most statement of your loops. 您可以在循环的最内层语句中“返回”。 This will cause it to return immediately, without looping through the rest of the values. 这将导致它立即返回,而不会循环遍历其余值。 If you want a list of values, you could do something like: 如果您想要一个值列表,则可以执行以下操作:

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

How about that? 那个怎么样?

for line in open('team.cfg','r'): list_fu = [ column for column in line.split() ] print(list_fu) 对于open('team.cfg','r')中的行:list_fu = [line.split()中列的列] print(list_fu)

Output: 输出:

['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']

if you want to read in those positions, you should now the column of the list EG -> Positions, so it is 1. now you catch the Teams and User with if conditions: 如果您想阅读这些位置,现在应该在列表EG->位置列表中,因此该列为1。现在,如果条件满足,您将捕获“团队”和“用户”:

for line in list_fu[1]: if line == 'goalkeeper': print(line) #do what you want to do 对于list_fu [1]中的行:如果line =='goalkeeper':print(line)#做您想做的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python文件获取值到html? - How can I get values from python file to my html? 从文件读取并获取值的Python脚本 - Python script to read from a file and get values 如何获得python程序以从表/外部文件读取并提供不同的数据值? - How can I get a python program to read and serve up different data values from a table/external file? 如何从python中的配置文件中读取argparser值? - how to read argparser values from a config file in python? Python:从.ini文件中读取所有配置值,并将其存储在Dictionary中,并在其他文件中进行访问 - Python: Read all config values from .ini file and store in Dictionary and access in other files 如何使用配置解析器从配置文件中读取多个值 - How to read muliple values from a config file using config parser 如何让我的python(2.5版)脚本在文件夹而不是命令行中运行jar文件? - How can I get my python (version 2.5) script to run a jar file inside a folder instead of from command line? 如何在python脚本中基于配置文件中的变量调用另一个python脚本? - How can I call another python script, in a python script, based on a variable in a config file? 从已编译的 python.exe 脚本中读取 config.py 文件 - Read config.py file from a compiled python .exe script 如何从python脚本中的ini文件读取和获取存储值? - How to read and get the stored value from ini file in python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM