Chris Y
Chris Y

Reputation: 3

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'"

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 !!!

Upvotes: 0

Views: 106

Answers (1)

Robert Levy
Robert Levy

Reputation: 29073

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. I think you mean for {} to be 0

Upvotes: 2

Related Questions