Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use json Module in python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the json module in python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Create a new python file named py3_json.py, and write the operation code in this file:

Import json

# define a json string people_str =''{"people": [{"name": "yale", "age": 25, "email": "yale@email.com"}, {"name": "McBag", "age": 30 "email": null}]}''# using json.loads () to load the string data = json.loads (people_str) print (data) # returns an python dictionary # {'people': [{' name': 'yale',' age': 25, 'email':' yale@email.com'} #, {'name':' McBag', 'age': 30 'email': None}]} print (type (data [' people'])) # returned # We read data for people in data ['people']: print (people) # print (people [' name']) # {'name':' yale', 'age': 25,' email': 'yale@email.com'} # {' name': 'McBag',' age': 30 'email': None} # Delete email information for people in data [' people']: del people ['email'] # use json.dumps () # to encode Python objects into a JSON string # indent indent display # sort_keys sort by key new_string = json.dumps (data,indent=1,sort_keys=True) print (new_string) # print result:''{"people": [{"name": "yale", "age": 25} {"name": "McBag", "age": 30}]}''# load json file with open ('people.json','r') as f: data = json.load (f) # print data for person in data [' people']: print (person ['name'], person [' age']) Person ['email']) # write to json file for person in data [' people']: del person ['email'] with open (' new_data.json','w') as wf: new_json = json.dumps (data) Indent=2) wf.write (new_json) # jsonimport refrom urllib.request import urlopen# that reads Sina News requests a Sina News address with urlopen (''http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=qbpdpl&top_time=20190531&top_show_num=100&top_order=DESC&js_var=comment_all_data''') as response: source = response.read () source = source.decode () # regular match Output json string part try: pattern = re.compile (r'\ {"conf\". *\}\]\}') matches = pattern.findall (source) data = json.loads (matches [0]) for item in data ['data']: print (item [' id']) Item ['title']) except Exception as e: print (' made a mistake', e) # part of the operation: # 5208 Evergrande with Aishen once represented Chinese football with Aishen why not # 5215 the candidate for light-skinned, straight-haired Miss India attracted netizens to follow # 5222 "Reunion" filmed through the strictest anti-abortion law Disney or will withdraw # 5229 the director of "Ice Breaking Operation" apologized for the logical Bug response to emotional drama superfluous.

Running result:

{'people': [{' name': 'yale',' age': 25, 'email':' yale@email.com'}, {'name':' McBag', 'age': 30,' email': None}]} {'name':' yale', 'age': 25,' email': 'yale@email.com'} {' name': 'McBag',' age': 30 'email': None} {"people": [{"age": 25, "name": "yale"}, {"age": 30, "name": "McBag"}]} yale 25 yale@email.comMcBag 30 NoneTestUser 30 testuser@163.com5208 the director of "Ice Breaking" apologized for the logical Bug response to the emotional drama of more than 5215 Curry 34-5 double-card 55 points and the Raptors took a 1-0 lead in the Warriors Finals. On how to use the json module in python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report