AttributeError:' list'对象没有属性' dlc'

时间:2015-01-15 10:33:08

标签: python dictionary printing attributeerror

我正在将数据流格式化为字典,然后尝试以更易读的格式将其打印出来。

然而,当试图运行函数/方法printMsg时,我得到一个错误,这对于我的生活我无法弄清楚。请参阅下面的代码

#!/usr/bin/python

#Imported Modules/Libraries

import re, os, subprocess
from time import sleep, clock
from collections import defaultdict
from multiprocessing import Process, Queue
import pprint
from os import system

#Global Declarations
last_time = 0.0
myDict = {}
count = defaultdict(int)
queueVar = Queue()

class PCANmsg(object):
    def __manline__(self):

        can_ID          = 0
        self.DLC        = 0
        self.CANtime    = 0
        self.PCANperiod = 0
        self.Count      = 0
        self.hdata0     = 0
        self.hdata1     = 0
        self.hdata2     = 0
        self.hdata3     = 0
        self.hdata4     = 0
        self.hdata5     = 0
        self.hdata6     = 0
        self.hdata7     = 0
        self.timing     = 0

def fillDict(lines):
    global myDict
    global last_time
    global count

    x = PCANmsg()

    for line in lines:
        start = clock()
        if(line[0].isdigit()):
            splitline = line.strip().split()

            del splitline[1:4]
            can_ID           = splitline[1]
            x.DLC            = splitline[2]
            x.CANtime        = splitline[0]
            x.PCANperiod     = 0
            x.Count          = count[can_ID] + 1  
            x.hdata0         = splitline[3]
            x.hdata1         = splitline[4]
            x.hdata2         = splitline[5]
            x.hdata3         = splitline[6]
            x.hdata4         = splitline[7]
            x.hdata5         = splitline[8]
            x.hdata6         = splitline[9]
            x.hdata7         = splitline[10]
            end = clock()
            x.timing         = (end - start) * 1000

            myDict[can_ID] = [x.DLC,
                              x.hdata0,
                              x.hdata1,
                              x.hdata2,
                              x.hdata3,
                              x.hdata4,
                              x.hdata5,
                              x.hdata6,
                              x.hdata7,
                              x.timing,
                              x.Count,
                              x.PCANperiod]

            start = end 
            printMsg(myDict, can_ID)


def printMsg(_dict,_id):

    print('%06X: %3d %8.2f %8.2F  %02X %02X %02X %02X %02X %02X %02X %02X %d' %
           (_id,
            _dict[_id].DLC,
            _dict[_id].CANtime,
            _dict[_id].PCANperiod,
            _dict[_id].int(count),
            _dict[_id].hdata0,
            _dict[_id].hdata1,
            _dict[_id].hdata2,
            _dict[_id].hdata3,
            _dict[_id].hdata4,
            _dict[_id].hdata5,
            _dict[_id].hdata6,
            _dict[_id].hdata7,
            _dict[_id].ELAPSED))

if __name__ == "__main__":

    system("echo \"i 0x011c e\" >/dev/pcan33")    

    procRX = subprocess.Popen('receivetest -f=/dev/pcan33'.split(), stdout=subprocess.PIPE)
    lines = iter(procRX.stdout.readline,"")

    fillDict(lines)

当代码执行时,运行正常运行printMsg(mydict,_id)函数调用:

Traceback (most recent call last):
  File "./manline.py", line 107, in <module>
    fillDict(lines)
  File "./manline.py", line 79, in fillDict
    printMsg(myDict, can_ID)
  File "./manline.py", line 86, in printMsg
    _dict[_id].DLC,
AttributeError: 'list' object has no attribute 'DLC'

任何人都可以告知造成这种情况的原因。我的目标也是能够在正确打印后并行运行fillDict和printMsg。

1 个答案:

答案 0 :(得分:0)

你的这段代码

myDict[can_ID] = [x.DLC,
                          x.hdata0,
                          x.hdata1,
                          x.hdata2,
                          x.hdata3,
                          x.hdata4,
                          x.hdata5,
                          x.hdata6,
                          x.hdata7,
                          x.timing,
                          x.Count,
                          x.PCANperiod]

它以列表的形式保存对ID的条目。你的代码片在哪里

def printMsg(_dict,_id):

    print('%06X: %3d %8.2f %8.2F  %02X %02X %02X %02X %02X %02X %02X %02X %d' %
       (_id,
        _dict[_id].DLC,
        _dict[_id].CANtime,
        _dict[_id].PCANperiod,
        _dict[_id].int(count),
        _dict[_id].hdata0,
        _dict[_id].hdata1,
        _dict[_id].hdata2,
        _dict[_id].hdata3,
        _dict[_id].hdata4,
        _dict[_id].hdata5,
        _dict[_id].hdata6,
        _dict[_id].hdata7,
        _dict[_id].ELAPSED))

期待它成为一本字典。

应该是

myDict[can_ID] = {}
myDict[can_ID]["DLC"]= bla bla
myDict[can_ID]["hdata0"] = bla bla
...
...
#So on so forth

然后您可以按照现在的方式继续打印。 或者你可以像这样打印

for key, val in _dict[_id].iteritems():
    print key, value