The difference between json and dict
- The Python dict is a data structure, a data transmission format is JSON. a json is prepared according to some agreed format pure string , it does not have any data structure of the feature. The manifestation of a string of rules dict python and json looks similar, but dict itself is a complete data structure to achieve all their own that some algorithm.
- dict the key Python hash may be any subject, json string only. Formally some similar, but json is plain text, not directly operate.
- json formatting requirements and can be used as a boundary symbol in double quotes or key values, not to use single quotes, and "key" have to use boundary character (double), but it does not matter dict.
- The Key python dict is unique, and the json Key can be repeated.
- The python dict can be nested tuple, json, only array.
json and conversion method in python dict where
Import json json.loads () # the json data into data dict json.dumps () # the data into json data dict the json.load () # read json file data, data converted into dict The json.dump () # dict json file write data into the data json
write python json
# Read json file, and converted dict with Open ( ' XXX.json ' , ' R & lt ' ) AS F: Data = the json.load (F) # or: Data = the json.load (Open ( ' XXX.json ' , ' R & lt ' )) # the dict data into json, json file and written with Open ( ' json_data.txt ' , ' W + ' ) aS F: the json.dump (dict_data, F)
data processing python dict
Take the key, the key-value pair:
# dictionary for the default iteration output only Key for K in dict_data: Print (K) # iteration output Key for K in dict_data.keys (): Print (K) # iteration output value for V in dict_data.values (): Print (V) # iteration output value pairs for K, V in dict_data.items (): Print (K, V)
dict CRUD: # increase in a key-value pair: dd [ ' new_key ' ] = ' XXX ' # change key dd [ ' Key ' ] = ' XX ' # delete the key for del [ ' Key ' ] # delete the key dictionary is a first layer del [ ' key1 ' ] [ ' key2 ' ] [ ' key3 ' ] # Erasing value pairs nesting in dd.pop ( ' Key ' ) # delete the key pair, and returns the corresponding value # is determined whether the dictionary format isinstance (data, dict)