Matéo Novello
Matéo Novello

Reputation: 3

can't get the value of something in a dict with a key

so i have this code

test = json.load(open('data.json'))
print (test)
print(test[MACadress])

and i can't get the value of MACadress, i already tried test.get(MACadress)

i get this error:

//this is test :

{'MACadress': '0x1c1bb5d3ce17', 'daate': '2020-03-19 17:33:19.715129', 'CPU': 2051.929375, 'mem': 26.0, 'disk': 57.1, 'process': 333, 'users': '1'}

//error message:

Traceback (most recent call last): File "recepteur_infos_machines.py", line 11, in print(test[MACadress]) NameError: name 'MACadress' is not defined

Upvotes: 0

Views: 38

Answers (3)

javier123454321
javier123454321

Reputation: 61

The dictionary key in this case is a string. (A word without quotations is a variable in python)

So if you write the following:

print(test['MACadress'])

It should print out the string: '0x1c1bb5d3ce17'

Upvotes: 0

Vitaliy
Vitaliy

Reputation: 411

You need to surround MACadress with hyphens, for python to treat it as a string. That is, the code should be:

test = json.load(open('data.json'))

print (test)

print(test['MACadress'])

Upvotes: 0

kpie
kpie

Reputation: 11110

print(test["MACadress"]) the key is a string. Without quotes you are referencing a non-existent variable.

Upvotes: 1

Related Questions