나는 사전에 저장된 값을 잡고 변수에 할당하는 믿을 수 없을 정도로 간단해야하는 일을하려고합니다.
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가 사전이라는 오류가 발생하는 이유를 설명하는 것 같습니다. 나는 당신이 의미 생각 {}
할 수0
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다