如何将从python字典检索到的值分配给变量?

如何将从python字典检索到的值分配给变量?,python,dictionary,Python,Dictionary,我正在尝试做一些非常简单的事情,就是抓取一个存储在字典中的值,然后把它赋给一个变量 current_bytes_total = DPIstats[applicationName]['Total Bytes'] # extract old byte count 这直接在解释器中工作,但当我试图在程序中执行此操作时,会出现以下错误: "newValue = new_bytes_total + current_bytes_total # add new byte count to old byt

我正在尝试做一些非常简单的事情,就是抓取一个存储在字典中的值,然后把它赋给一个变量

current_bytes_total = DPIstats[applicationName]['Total Bytes'] # extract old byte count
这直接在解释器中工作,但当我试图在程序中执行此操作时,会出现以下错误:

"newValue = new_bytes_total + current_bytes_total   # add new byte count to old byte count
TypeError:不支持+:“int”和“dict”的操作数类型

是否有方法检索字典中存储的值,以便将其分配给变量

粘贴所有内容,以防在此之前我做了一些阻止它工作的事情

def getFileName(filename):
    file_contents = open(filename,'rU')
    DPIstats={}   # create empty dictionary to hold application name to byte values
    for line in file_contents:
        values = line.split() # split each line on white space and put each lines values into a list
        # print(values)
        # uncomment print(values)to test the values in my data structure
        if 'End:' in values:            # if 'End:' in values then this is an end record
                                        # grab the values in the list for positions [-4] (bytes sent)
                                        # and [-2] (bytes received) and store below
            applicationName = values[14]    # type is string
            if applicationName in DPIstats: # if application name key already exists do nothing
                pass
            else:                           # if application name doesn't exist create a new dict entry
                DPIstats[applicationName]= {}
                DPIstats[applicationName]['Total Bytes'] = {}
            bytes_sent = 0
            bytes_received = 0
            current_bytes_total = 0
            new_bytes_total = 0
            newValue = 0
            bytes_sent += int(values[-4])     # convert to an integer
            bytes_received += int(values[-2]) # convert to an integer
            new_bytes_total = bytes_sent + bytes_received  # get new byte count from current entry
            current_bytes_total = DPIstats[applicationName]['Total Bytes'] # extract old byte count
            newValue = new_bytes_total + current_bytes_total   # add new byte count to old byte count
            DPIstats[applicationName]['Total Bytes'] = newValue    # assign new value to Total Bytes stored for the application name
file_contents.close()      # close the file

def main():
    filename = sys.argv[1]     # get the first command line argument and assign
    getFileName(filename)      # call and feed specified filename

if __name__ == '__main__':
    main()                     # call the main function to get things started

提前谢谢!!!

我不知道这是什么语言,但是

DPIstats[applicationName]['Total Bytes']={}


…似乎可以解释为什么TotalBytes作为字典时出错。我想你是指
{}
to be
0

是的,“Total Bytes”是一个字典。我正在尝试检索存储在“Total Bytes”中的当前值并将其分配给变量。Robert,谢谢。我没有听清您最初说的话,但是的,您是正确的。我应该为其分配一个值0,而不是创建另一个字典。在上次运行时修复该问题我已经决定了。谢谢!