将元组添加到元组列表中

问题描述 投票:0回答:1

我正在尝试使用python3自动执行一些DBA任务。

x = "       @@Hostname: host1"
y = "@@innodb_buffer_pool_size: 1"
z = "  @@Max_connections: 150"
op = {}
a = tuple(x.split(':'))
b = tuple(y.split(':'))
c = tuple(z.split(':'))
host=""
if (a[0].strip()).lower() == "@@hostname" and (a[1].strip()).lower() not in op:
    host = a[1].strip()
    op[host] = []
if (b[0].strip()).lower() == "@@innodb_buffer_pool_size" and int(b[1].lstrip())<2:
    #z = b[0].strip().lstrip('@@'),b[1].strip()
    op[host].append((b[0].strip().lstrip('@@'),b[1].strip()))
if (c[0].strip()).lower() == "@@Max_connections" and int(c[1].lstrip())<152:
    op[host].append((c[0].strip().lstrip('@@'),c[1].strip()))   
#elif (a[0].strip()).lower() == "@@log_bin" and int(a[1].strip()) == 0:
 #   op[host].append(tuple((a[0].strip()).lstrip('@@'),a[1].strip()))
#elif (a[0].strip()).lower() == "@@expire_logs_days" and int(a[1].strip()) == 0:
 #   op[host].append(tuple((a[0].strip()).lstrip('@@'),a[1].strip()))
#else:
 #   pass
#print (c)
print (op)

我得到的输出:

{'host1': [('innodb_buffer_pool_size', '1')]}

我期望的输出:

{'host1': [('innodb_buffer_pool_size', '1'),('max_connections','150')]}

[如果您查看我的代码,我的第一个append语句会将元组追加到一个空列表。但是我的第二个追加没有追加到列表的元组中。我不明白为什么自第一个python项目以来就出现了这种行为,以及应该如何将元组附加到字典中特定键的现有列表中。

这只是脚本的一部分,我试图遍历几个文件并为每个主机构造一个元组列表,每个唯一的主机都是键,因此构造了一个字典。谢谢

python
1个回答
0
投票

[遇到这种情况时,更倾向于调试和拆分代码,这是使用Max_connections的问题,因为这是缺少的]

打印两个条件时我们有

print((c[0].strip()).lower() == "@@Max_connections", int(c[1].lstrip()) < 152)  # False True

然后再往前看,将值设置为小写,但测试字串包含大写:不是有效值

更正

if (c[0].strip()).lower() == "@@max_connections" and int(c[1].lstrip()) < 152:
© www.soinside.com 2019 - 2024. All rights reserved.