简体   繁体   中英

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)

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:

for line in list_fu[1]: if line == 'goalkeeper': print(line) #do what you want to do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM