简体   繁体   English

如何将从python字典中检索的值赋给变量?

[英]how to assign value retrieved from a python dictionary to a variable?

I'm trying to do something which should be incredibly simple which is just grab a value stored in a dictionary and then assign it to a variable. 我正在尝试做一些应该非常简单的事情,它只是抓取存储在字典中的值然后将其分配给变量。

current_bytes_total = DPIstats[applicationName]['Total Bytes'] # extract old byte count

This works in the interpreter directly, but when i attempt to do this inside a program it errors with the following: 这直接在解释器中工作,但是当我尝试在程序中执行此操作时,它会出现以下错误:

"newValue = new_bytes_total + current_bytes_total   # add new byte count to old byte count

TypeError: unsupported operand type(s) for +: 'int' and 'dict'" TypeError:+:'int'和'dict'不支持的操作数类型

Is there a way to retrieve the value stored in the dictionary such that it can be assigned to a variable? 有没有办法检索存储在字典中的值,以便可以将其分配给变量?

Pasting everything in case there is something I've done before this which is preventing it from working. 粘贴所有东西以防万一我在此之前做过的事情阻止它工作。

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

Thanks in advance !!! 提前致谢 !!!

I don't know what language this is but... 我不知道这是什么语言但......

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

... would seem to explain why you get an error about TotalBytes being a dictionary. ...似乎解释了为什么你得到关于TotalBytes是字典的错误。 I think you mean for {} to be 0 我认为你的意思是{}0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将python字典中的值赋给变量 - unable to assign value from python dictionary to variable 如何将变量分配给 Python 字典中的值? - How can I assign a variable to a value in a dictionary in Python? 如何在 Python 中将字典值分配给变量? - How do I assign a dictionary value to a variable in Python? Python字典:将值作为变量分配给键 - Python dictionary: Assign value as variable to key 是否可以将变量分配为字典中的值(对于 Python)? - Is it possible to assign a variable as a value in a dictionary (for Python)? 如何仅将变量中的数据分配给Dictionary中的值? - How to assign only data from a variable to the value in Dictionary? 如何从字符串中提取变量名和值并分配给 python 中的变量 - How to extract variable name and value from string and assign to variable in python 在 Python 中,如何将变量的值分配给字典,其中变量将在每次迭代中不断获取值 - In Python, How to assign value of variable to the dictionary, where the variable will keep getting values for each iteration 如何在Python中将字符串值分配为空字典? - How to assign a string value as an empty dictionary in Python? Python-如何从 tkinter 小部件获取值并将其分配给变量 - Python-How to get the value from tkinter widget and assign it to a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM