python - 如何在python中更新json文件
问题描述
我是 python 的初学者,我想知道如何通过添加另一个名为 cat 的属性来更新 python 中的 json 文件,该属性是特定类别,如运动、教育、音乐...等
任何帮助将不胜感激谢谢
这是我的 json 文件数据的示例:
前 :
{"tweet_id":"1261276320878788609",
"date":"Fri May 15 12:44:42 +0000 2020",
"raw_text":"برنامج وطني لدعم المبدعين في مواجهة #كورونا",
"geo_source":"user_location",
"location":{"address":
{"country":"Tunisia","country_code":"tn","state_district":"غزالة","county":"العرب","state":"Bizerte"},
"response":
"{'place_id': 235309103, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'relation', 'osm_id': 7124228, 'boundingbox': ['37.105957', '37.2033466', '9.4739053', '9.6124953'], 'lat': '37.1551868', 'lon': '9.54834183807249', 'display_name': 'العرب, غزالة, Bizerte, Tunisia', 'class': 'boundary', 'type': 'administrative', 'importance': 0.45, 'icon': '/data/nominatimimages/mapicons/poi_boundary_administrative.p.20.png','address':{'county': 'العرب', 'state_district': 'غزالة', 'state': 'Bizerte', 'country': 'Tunisia', 'country_code': 'tn'}}",
"geohash":"snwg37buskzd","query_term":"arab","lon":9.54834183807249,"lat":37.1551868},
"user_friends_count":61,"user_description":"I love UAE and his great leadership",
"user_created_at":"Wed Oct 09 11:41:41 +0000 2013",
"user_screen_name":"SikandarMirani",
"user_id_str":"706377881",
"user_verified":false,
"user_statuses_count":50804,
"user_followers_count":946,
"user_location":"Dubai United Arab Emirates"}
后 :
{"tweet_id":"1261276320878788609",
"date":"Fri May 15 12:44:42 +0000 2020",
"raw_text":"برنامج وطني لدعم المبدعين في مواجهة ##البيان_القارئ_دائما",
"geo_source":"user_location",
"location":{"address":
{"country":"Tunisia","country_code":"tn","state_district":"غزالة","county":"العرب","state":"Bizerte"},
"response":
"{'place_id': 235309103, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'relation', 'osm_id': 7124228, 'boundingbox': ['37.105957', '37.2033466', '9.4739053', '9.6124953'], 'lat': '37.1551868', 'lon': '9.54834183807249', 'display_name': 'العرب, غزالة, Bizerte, Tunisia', 'class': 'boundary', 'type': 'administrative', 'importance': 0.45, 'icon': '/data/nominatimimages/mapicons/poi_boundary_administrative.p.20.png','address':{'county': 'العرب', 'state_district': 'غزالة', 'state': 'Bizerte', 'country': 'Tunisia', 'country_code': 'tn'}}",
"geohash":"snwg37buskzd","query_term":"arab","lon":9.54834183807249,"lat":37.1551868},
"user_friends_count":61,"user_description":"I love UAE and his great leadership",
"user_created_at":"Wed Oct 09 11:41:41 +0000 2013",
"user_screen_name":"SikandarMirani",
"user_id_str":"706377881",
"user_verified":false,
"user_statuses_count":50804,
"user_followers_count":946,
"user_location":"Dubai United Arab Emirates"
"cat":"(any category)"}
解决方案
最简单的方法是将json文件加载到程序中,添加参数然后转储它。
import json
with open("myfile.txt") as my_file:
my_json_dict = json.load(my_file)
my_json_dict["New key"]="New value"
with open("myfile.txt', "w") as my_file:
json.dump(my_json_dict,my_file)
推荐阅读
- javascript - 如何将查询调用到脚本部分
- reactjs - 反应选择值落后一步
- python - 值为 x 的 Python bisect 是长度为 1 的数组
- angular - 自定义 rxjs 商店的选择器功能
- reactjs - 从 getServerSideProps getState() 或 useSelector() 中获取状态值的正确方法?
- java - 在合并方法中将一个数组分配给另一个数组
- python - 选择一个有效的选择 Django Filtered Drop Down Menu
- javascript - 如何根据正则表达式值和其他几个字段对值求和?
- sql - 每当 table1 的列中的数据在 postgresql 中更新时,如何自动将 table1 的列名插入 table2?
- mongodb - 在 mongo shell 中获取存储在 MongoDB 中的 BSON 类型的文档字段