我有幾個配置文件。一個有一些團隊名稱,另一個有職位。我的每個用戶都會上傳一個包含團隊和職位的文件。團隊和職位必須與配置文件中的內容相匹配,否則會打印錯誤。我可以讓腳本從每個配置文件中只讀取一個值。它不會讀取其餘的值。我怎樣才能讀取配置文件中的所有值?如何讓我的Python腳本讀取配置文件中的所有值?
這裏有兩個配置文件: 爲球隊: [隊伍] 團隊=巴塞羅那,拜仁,國米,切爾西
的位置:
[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)
好吧我試過這樣,創建一個調用變量,追加它並返回它,我仍然得到相同的結果 – bayernmunich