AttributeError: 'list' object has no attribute 'dlc'

AttributeError: 'list' object has no attribute 'dlc'

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

然而,当尝试 运行 function/method 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 并行。

你的这段代码

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