[英]Python will not pull values from dictionary properly using for loop
Im trying to use a dictionary to check a given number of servers listed for a particular SQL backup success or fail. 我试图使用字典检查为特定SQL备份成功或失败列出的给定数量的服务器。 My problem so far is when I run this code: 到目前为止,我的问题是当我运行此代码时:
for serverChk in srvrDict['Server']:
it returns the server name as single characters on each new line like: 它将服务器名称作为单个字符返回到每个新行,如:
S 小号
E Ë
R [R
V V
E Ë
R [R
So in my trial I see this "Error connecting to T to check OS version" where T is the fist character of the servername. 因此,在我的试用版中,我看到“连接到T以检查操作系统版本时出错”,其中T是服务器名称的第一个字符。 I can't seem to put my finger on it and all the searching I've done has lead me to asking. 我似乎无法把手指放在上面,我所做的所有搜索都引出了我的要求。 Thanks! 谢谢!
class checkstatus:
#def getServers(self):
chkbkpstats = csv.reader(file('c://temp//networkerservers.csv'))
for row in chkbkpstats:
srvrDict = {}
srvrDict['Server'] = row[0]
srvrDict['Instance'] = row[1]
print srvrDict
for serverChk in srvrDict['Server']:
try:
c = wmi.WMI(server)
for os in c.Win32_OperatingSystem():
osVer = os.caption
except:
print 'Error connecting to %s to check OS version' % serverChk
if '2003' in osVer:
print 'w2k3'
if '2008' in osVer:
print 'w2k8'
I suppose you have stored a string in your dictionary. 我想你已经在字典中存储了一个字符串。 So the line for serverChk in srvrDict['Server']
translates to for serverChk in yourSavedString
. 因此for serverChk in srvrDict['Server']
的行转换for serverChk in yourSavedString
。 This is why you are getting individual characters. 这就是你获得个性化角色的原因。 To access individual dictionary items you should do for k,v in srvrDict.iteritems()
where k
is the key and v
is the value. 要访问单独的字典项,您应该for k,v in srvrDict.iteritems()
中for k,v in srvrDict.iteritems()
其中k
是键, v
是值。
You are overwriting the Server
and Instance
values in srvrDict
each iteration of your loop through chkbkpstats
, not actually generating a sequence of data with an entry for each item in your log file as it appears you expect. 您通过chkbkpstats
在循环的每次迭代中覆盖srvrDict
的Server
和Instance
值,而不是实际生成一个数据序列,其中包含日志文件中每个项目的条目,如您所期望的那样。 You need to make that a list containing dictionaries, which you append to each iteration. 您需要将包含字典的列表添加到每个迭代中。 You are probably looking for something more like: 你可能正在寻找更像的东西:
chkbkpstats = csv.reader(file('c://temp//networkerservers.csv'))
srvrs = []
for for row in chkbkpstats:
srvrs.append({'Name' : row[0], 'Instance' : row[1]})
for srvr in srvrs:
try:
c = wmi.WMI(srvr['Instance'])
except:
print 'Error connecting to %s to check OS version' % srvr['Name']
else:
osVer = c.Win32_OperatingSystem()[0].Caption
if '2003' in osVer:
print 'w2k3'
elif '2008' in osVer:
print 'w2k8'
There are a few problems with your code. 您的代码存在一些问题。
First, you create a new srvrDict
each time you go through the first for
loop, overwriting the value that was stored in this variable the last time. 首先,每次执行第一个for
循环时都会创建一个新的srvrDict
,并覆盖上次存储在此变量中的值。 I think, what you actually intended to do is the following: 我想,你实际打算做的是以下几点:
srvrDict = {}
for row in chkbkpstats:
srvrDict[row[0]] = row[1]
Now, srvrDict
will contain an entry like {'P1RT04': ['THP06ASU']}
for each row in chkbkpstats
, mapping server names to lists of instances running on that server. 现在, srvrDict
将为{'P1RT04': ['THP06ASU']}
每一行包含类似{'P1RT04': ['THP06ASU']}
的chkbkpstats
,将服务器名称映射到在该服务器上运行的实例列表。
Then, in the second loop, use for serverChk in srvrDict:
to iterate over all the entries in the dictionary. 然后,在第二个循环中, for serverChk in srvrDict:
使用for serverChk in srvrDict:
迭代字典中的所有条目。 However, I'm not sure where the variable server
in c = wmi.WMI(server)
comes from. 但是,我不确定c = wmi.WMI(server)
中的变量server
来自c = wmi.WMI(server)
。 If this is what has been row[1]
in the first loop, then you should use srcvDict[serverChk]
to retrieve the value from the dictionary. 如果这是第一个循环中的row[1]
,那么你应该使用srcvDict[serverChk]
从字典中检索值。
This way, the whole procedure would look something like this: 这样,整个过程看起来像这样:
chkbkpstats = csv.reader(file('c://temp//networkerservers.csv'))
srvrDict = {}
for row in chkbkpstats:
name, instance = row[0], row[1]
if name not in srvrDict:
srvrDict[name] = []
srvrDict[name].append(instance)
for server in srvrDict:
for instance in srvrDict[server]:
try:
c = wmi.WMI(instance)
except:
print 'Error connecting to %s to check OS version' % server
else:
osVer = c.Win32_OperatingSystem()[0].caption
if '2003' in osVer:
print 'w2k3'
elif '2008' in osVer:
print 'w2k8'
else:
print 'unknown OS'
PS.: I'm not sure what's the return value of c.Win32_OperatingSystem()
. PS。:我不确定c.Win32_OperatingSystem()
的返回值是c.Win32_OperatingSystem()
。 [...] Update: Thanks to sr2222 for pointing this out. [...] 更新:感谢s2222指出这一点。 Code fixed. 代码已修复。
Update: Edited the code to allow for one server hosting multiple instances. 更新:编辑代码以允许一个服务器托管多个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.