加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 更新字典而不覆盖现有的键值对?

发布时间:2020-12-20 13:16:26 所属栏目:Python 来源:网络整理
导读:我有一个输入文件,我可以从中构建我的字典 一般格式 IP_1KEY_1=VALUE_1KEY_2=VALUE_2IP_2KEY_1=VALUE_1KEY_2=VALUE_2 例 192.168.1.1USER_NAME=adminPASSWORD=admin123192.168.1.2USER_NAME=userPASSWORD=user123 预期字典应如下所示: print dictionary_of_
我有一个输入文件,我可以从中构建我的字典

一般格式

<IP_1>
KEY_1=VALUE_1
KEY_2=VALUE_2

<IP_2>
KEY_1=VALUE_1
KEY_2=VALUE_2

192.168.1.1
USER_NAME=admin
PASSWORD=admin123

192.168.1.2
USER_NAME=user
PASSWORD=user123

预期字典应如下所示:

>>print dictionary_of_ip
{'192.168.1.1':{'USER_NAME'='admin','PASSWORD'='admin123'},'192.168.1.2':{'USER_NAME'='user','PASSWORD'='user123'}}

本质上是字典中的字典

以下是我的代码:

def generate_key_value_pair(filePath,sep='='):
    dict_of_ip = {}
    slave_properties = {}
    with open(filePath,"rt") as f:
        for line in f:
            stripped_line = line.strip()
            if stripped_line and stripped_line[0].isdigit():
                #print 'Found Ip'
                ip = stripped_line
                dict_of_ip[ip] = ''
            elif stripped_line and stripped_line[0].isupper():
                #print "Found attributes")
                key_value = stripped_line.split(sep)
                key = key_value[0].strip()
                value = key_value[1].strip()
                slave_properties[key] = value
                dict_of_ip[ip] = slave_properties

    return dict_of_ip

我能够按预期获得第一个IP及其属性,但第二个IP的第二组值将覆盖第一个IP.

>>print dict_of_ip
{'192.168.1.1': {'USER_NAME': 'user','PASSWORD': 'user123'},'192.168.1.2': {'USER_NAME': 'user','PASSWORD': 'user123'}}

dict_of_ip [ip] = slave_properties导致覆盖.如何防止’192.168.1.2’键中的值覆盖第一个?

解决方法

试试这个:

def generate_key_value_pair(filePath,sep='='):
    dict_of_ip = {}
    with open(filePath,"rt") as f:
        for line in f:
            stripped_line = line.strip()
            if stripped_line and stripped_line[0].isdigit():
                #print 'Found Ip'
                slave_properties = {}
                ip = stripped_line
                dict_of_ip[ip] = ''
            elif stripped_line and stripped_line[0].isupper():
                #print "Found attributes")
                key_value = stripped_line.split(sep)
                key = key_value[0].strip()
                value = key_value[1].strip()
                slave_properties[key] = value
                dict_of_ip[ip] = slave_properties

    return dict_of_ip

你在哪里使用相同(修改)的字典.我没有故意更改您的代码逻辑,只是将slave_properties = {}更改为应该的位置

您甚至可以删除slave_properties并单独使用dict

def generate_key_value_pair(filePath,"rt") as f:
        for line in f:
            stripped_line = line.strip()
            if stripped_line and stripped_line[0].isdigit():
                #print 'Found Ip'
                ip = stripped_line
                dict_of_ip[ip] = {}
            elif stripped_line and stripped_line[0].isupper():
                #print "Found attributes")
                key_value = stripped_line.split(sep)
                key = key_value[0].strip()
                value = key_value[1].strip()
                dict_of_ip[ip][key] = value

    return dict_of_ip

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读