Skip to main content
 首页 » 编程设计

python之如何从 JSON 文件中删除键/对象

2024年11月24日9cmt

我想进入 JSON 文件并找到 "class": "DepictionScreenshotsView", 并将其完全删除或将其更改为 "class": "",我已经在这里工作了几个小时但没有运气我已经尝试过谷歌搜索但没有任何帮助,欢迎任何帮助!

编辑:如果有帮助,我们正在查看 elif new_url == ""。

代码:

#!/usr/bin/env python3 
import os 
 
# Load the data 
file_name = "path/to/json/file" 
with open(file_name) as fh: 
    full_data = json.load(fh) 
 
screen_shots = full_data['tabs'][0]['views'][3]['screenshots'] 
 
for number, screen_shot in enumerate(screen_shots): 
    new_url = input("Screnshot URL: ").strip() 
 
    if new_url: 
        screen_shot.update({"url": new_url, "fullSizeURL": new_url}) 
    elif new_url == "": 
 
        #for k, v in full_data.items(): 
        #    if isinstance(v, dict) and v.get("class") == "DepictionScreenshotsView": 
        #        full_data.pop(k) 
        #        break 
 
        #json_lines = [] 
        #for line in fh.readlines(): 
        #    j = json.loads(line) 
        #    if not j['class'] == 'DepictionScreenshotsView': 
        #        json_lines.append(line) 
 
        full_data['tabs'][0]['views'][3]['screenshots'] = screen_shots 
        full_data['tabs'][0]['views'][3]['class'] = screen_shots 
    else: 
        break 
 
with open(file_name, 'w') as fh: 
    json.dump(full_data, fh, indent=4) 

JSON 文件:

{ 
   "minVersion": "0.1", 
   "class": "DepictionTabView", 
   "tintColor": "#2cb1be", 
   "headerImage": "", 
   "tabs": [ 
      { 
         "tabname": "Details", 
         "class": "DepictionStackView", 
         "tintColor": "#2cb1be", 
         "views": [ 
            { 
               "class": "DepictionSubheaderView", 
               "useBoldText": true, 
               "useBottomMargin": false, 
               "title": "Description" 
            }, 
            { 
               "class": "DepictionMarkdownView", 
               "markdown": "Some dummy text...", 
               "useRawFormat": true 
            }, 
            { 
               "class": "DepictionSeparatorView" 
            }, 
            { 
               "class": "DepictionSubheaderView", 
               "useBoldText": true, 
               "useBottomMargin": false, 
               "title": "Screenshots" 
            }, 
            { 
               "class": "DepictionScreenshotsView", 
               "itemCornerRadius": 6, 
               "itemSize": "{160, 284.44444444444}", 
               "screenshots": [ 
                  { 
                     "accessibilityText": "Screenshot", 
                     "url": "http://example.com/image.png" 
                  } 
               ] 
            }, 
            { 
               "class": "DepictionSeparatorView" 
            }, 
            { 
               "class": "DepictionSubheaderView", 
               "useBoldText": true, 
               "useBottomMargin": false, 
               "title": "Information" 
            }, 
            { 
               "class": "DepictionTableTextView", 
               "title": "Author", 
               "text": "User" 
            }, 
            { 
               "class": "DepictionSpacerView", 
               "spacing": 16 
            }, 
            { 
               "class": "DepictionStackView", 
               "views": [ 
                  { 
                     "class": "DepictionTableButtonView", 
                     "title": "Contact", 
                     "action": "http://example.com/", 
                     "openExternal": true 
                  } 
               ] 
            }, 
            { 
               "class": "DepictionSpacerView", 
               "spacing": 16 
            } 
         ] 
      }, 
      { 
         "tabname": "History", 
         "class": "DepictionStackView", 
         "views": [ 
            { 
               "class": "DepictionSubheaderView", 
               "useBoldText": true, 
               "useBottomMargin": false, 
               "title": "" 
            }, 
            { 
               "class": "DepictionMarkdownView", 
               "markdown": "<ul>\n<li>Initial release.<\/li>\n<\/ul>", 
               "useRawFormat": true 
            } 
         ] 
      } 
   ] 
} 

请您参考如下方法:

更容易将数据读入字典然后遍历它并删除有问题的键

import os 
import json 
 
 
file_name = "/tmp/jsonfile.json" 
with open(file_name) as fh: 
    full_data = json.load(fh) 
    fh.close() 
 
for tab in full_data['tabs']: 
    for view in tab['views']: 
        if view['class'] == 'DepictionScreenshotsView': 
            del view['class'] 
        if 'screenshots' in view: 
            view['screenshots'] = [] 
 
print json.dumps(full_data)