Read and store dict() And .json Format file
Read .json Format file and save data to dictionary
Save dictionary data to .json In file
Garbled code when outputting dictionaries on the command line
Convert string data to dictionary data
take dict Data writing json In file
Read and store dict() And .json Format file Read .json Format file and save data to dictionaryData files :hg.json
{" Business name ": " Hong Kong Style porridge hot pot with a delicious taste ( The sports shop )", " score ": 27.0, " Address ": " C, East Road, hotpot worker stadium 2 China Red Street 3 building 2 In the layer ", " Per capita consumption ": 174, " Number of comments ": 2307}{" Business name ": " Jingge Laozao hot pot ( Wangjing New World store )", " score ": 26.2, " Address ": " Hotpot Wangjing Guangshun South Street Road 16 Number ", " Per capita consumption ": 105, " Number of comments ": 1387}{" Business name ": " Facebook Hong Kong hotpot ( Jiuxianqiao Lido store )", " score ": 24.5, " Address ": " Hot pot Fangyuan West Road 6 First floor No ", " Per capita consumption ": 218, " Number of comments ": 39}
For the above data , The following methods can be used to json Encoded string converted to python data structure dict:
# -*- coding: utf-8 -*-import jsonimport codecsdata = []with codecs.open("hg.json", "r", "utf-8") as f: for line in f: dic = json.loads(line) data.append(dic) print(json.dumps(dic, indent=4, ensure_ascii=False, encoding='utf-8'))
Save dictionary data to .json In file dic = {" Business name ": " Jingge Laozao hot pot ( Wangjing New World store )", " score ": 26.2, " Address ": " Hotpot Wangjing Guangshun South Street Road 16 Number ", " Per capita consumption ": 105, " Number of comments ": 1387}with codecs.open('hg.json','a', 'utf-8') as outf: json.dump(dic, outf, ensure_ascii=False) outf.write('\n')
Garbled code when outputting dictionaries on the command line If there is Chinese in the dictionary data ,print dic Can not display Chinese normally , You can format the output dictionary data in the following way :
dic = {" Beijing ": [446, 208.7, 110000], " tianjin ": [454.2, 219.8, 120000], " Shanghai ": [498.6, 319.7, 310000]}print(json.dumps(dic, ensure_ascii=False, encoding='utf-8', indent=4))
Convert string data to dictionary data Two conversion methods
user = "{'name' : 'LiHua', 'sex' : 'male', 'age': 18}"dic1 = eval(user)exec("dic2="+user)
Add
Generally speaking ,json When decoding, a dictionary or list will be created from the provided data , If you want to create other types of objects , It can be for json.loads() Method provides object_pairs_hook perhaps object_hook Parameters . The following example shows how we should json The data is decoded as OrderedDict( Orderly dictionary ), This keeps the order of the data unchanged .
>>> s = '{"name":"ACME", "SHARES":50, "PRICE":490}'>>> from collections import OrderedDict>>> data = json.load(s, object_pairs_hook=OrderedDict)>>> dataOrderedDict([('name', 'ACME'), ('shares', 50), ('price', 490)]>>>
take dict Data writing json In file Now get the data of a medical website , Finally, it's converted into dict type , Data needs to be written to JSON In file , To facilitate the use of the following data
with open('./medical.json', 'w',encoding='utf-8') as fp: json.dump(data, fp)
But the final data is like this :
It should be a normal Chinese string , nevertheless ASCII code , So in dump Method ensure_ascii Parameters , as a result of dump() Method to convert a dictionary to a string , Will default to unicode Code to ascii The encoding mode is input into the string
with open('./medical.json', 'w',encoding='utf-8') as fp: json.dump(data, fp,ensure_ascii=False)
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .