wiliiwin 发表于 2014-11-17 16:46

字典赋值的报错

本帖最后由 wiliiwin 于 2014-11-17 17:23 编辑

# more Master.conf

host=192.168.100.62
source=/home/master/tools
md5sum=/home/master/tools/md5sum
version=1.0.4


host=192.168.100.61
user=jqa
tomcat=/home/jqa/opt/apache-tomcat-6.0.41/webapps
db2=db2inst@db2inst


host=192.168.100.15
user=jqa
tomcat=/home/jqa/opt/apache-tomcat-6.0.41/webapps
db2=db2inst@db2inst我想把此文件的[]里面的内容和=左边的值作为字典的key,和perl里面的哈希的哈希相似的字典,下面是我的代码#!/usr/bin/python
import re

hash={}
fd=open('Master.conf','r')
forline infd:
   flag=0
   matchObj=re.match('\[(\w+)\]',line)
   if matchObj:
          outer_key=matchObj.group(1)   
   elif flag==0:
          matchobj=re.match('(.*?)=(.*)',line)
          if matchobj:
             hash=matchobj.group(2)
#print hash 执行这个脚本的时候,报了一个错误, 1.py
Traceback (most recent call last):
File "./1.py", line 14, in ?
    hash=matchobj.group(2)
KeyError: 'Master'说是错误的键,不知道为什么会有这样的报错呢?请指教。

Linux_manne 发表于 2014-11-17 17:18

本帖最后由 Linux_manne 于 2014-11-17 17:19 编辑

你这个hash 是dict 怎么能hash[][]
另外这个配置文件 为什么不用configparser 这个模块? 用文件读取多此一举..

wiliiwin 发表于 2014-11-17 17:26

回复 2# Linux_manne

我之前学的是perl,现在才学习python,不想用模块,就想用自己的办法来完成一些文本处理,也是一种熟练python的一个过程。


你说的字典,我是要完成这样的字典

hash={xxx:{yyy:zzz}}

xxx等于是[]里面的内容,yyy等于是=左边的数值,zzz是=右边的数值

因此就出现了hash=
不知道你知道你清楚了吗?



   

Linux_manne 发表于 2014-11-17 17:29

本帖最后由 Linux_manne 于 2014-11-17 17:31 编辑

hash = {matchobj.group(1):matchobj.group(2)}
但是由于你outer_key 下面有3个 所以都会被最后一个覆盖..

wiliiwin 发表于 2014-11-17 17:37

本帖最后由 wiliiwin 于 2014-11-17 17:39 编辑

回复 4# Linux_manne
覆盖不了的键不一样啊怎么会覆盖   我最后有一个打印的语句,我只是要提现出来打印结果。


   

Linux_manne 发表于 2014-11-17 17:39

回复 5# wiliiwin


   那种形式差不多就是你要得吧? 自己在修修

609854 发表于 2014-11-17 19:37

回复 1# wiliiwin

你的问题的原因是value 没有追加,总是覆盖前一个的值。#!/user/bin/python
import re

dictory = {}
dict1 = {}

fd=open("data.txt")

for line in fd.readlines():
    matchObj = re.search('\[(.+)\]', line)
    if matchObj:
      outer_key = matchObj.group(1)
      #print outer_key
    else:
      matchobj = re.search('(.*)\s*=\s*(.*)', line)
      if matchobj:
            key = matchobj.group(1)
            #print key
            value = matchobj.group(2)
            #print value
            dict0 = {}
            dict0 = {key : value}
            dict1 = dict( dict0.items() + dict1.items() )
            dictory = dict1
            
print dictory
print dictory['VIS']
print dictory['VIS']['host']{'QA': {'tomcat': '/home/jqa/opt/apache-tomcat-6.0.41/webapps', 'md5sum': '/home/master/tools/md5sum', 'source': '/home/master/tools', 'host': '192.168.100.62', 'version': '1.0.4', 'user': 'jqa', 'db2': 'db2inst@db2inst'}, 'VIS': {'tomcat': '/home/jqa/opt/apache-tomcat-6.0.41/webapps', 'md5sum': '/home/master/tools/md5sum', 'source': '/home/master/tools', 'host': '192.168.100.62', 'version': '1.0.4', 'user': 'jqa', 'db2': 'db2inst@db2inst'}, 'Master': {'source': '/home/master/tools', 'host': '192.168.100.62', 'version': '1.0.4', 'md5sum': '/home/master/tools/md5sum'}}
{'tomcat': '/home/jqa/opt/apache-tomcat-6.0.41/webapps', 'md5sum': '/home/master/tools/md5sum', 'source': '/home/master/tools', 'host': '192.168.100.62', 'version': '1.0.4', 'user': 'jqa', 'db2': 'db2inst@db2inst'}
192.168.100.62
   

wiliiwin 发表于 2014-11-17 19:52

回复 7# 609854
不是追加的问题,我打印是在循环里面的。现在问题是报key的错误。


   

wiliiwin 发表于 2014-11-17 20:16

本帖最后由 wiliiwin 于 2014-11-17 20:35 编辑

python貌似不支持直接用dict[][]这种方式来赋值的。#!/usr/bin/perl
my $hash;

$hash{'a'}{'b'}='c';
$hash{'a'}{'e'}='f';
printf"$hash{'a'}{'b'}\n";
printf"$hash{'a'}{'e'}\n"; # ./1.pl
c
f# more test.py
#!/usr/bin/python

dict={}

dict['a']['b']='c'
print dict['a']['b']

# ./test.py
Traceback (most recent call last):
File "./test.py", line 5, in ?
    dict['a']['b']='c'
KeyError: 'a'

Linux_manne 发表于 2014-11-18 09:11

本帖最后由 Linux_manne 于 2014-11-20 09:13 编辑

回复 9# wiliiwin


   哦 之前说错了。。 是可以那样附值的... 只是你先要构造出{key:{key:vaule}} 这种形式, 单独{} 这样的形式 d[][] 肯定会不对.
页: [1] 2
查看完整版本: 字典赋值的报错