简体   繁体   English

从 json 文件中删除所有 xmlns 标签

[英]Remove all xmlns tag from json file

I try to remove all xmlns tag from my json file.我尝试从我的 json 文件中删除所有 xmlns 标签。 How to do it only with xmltodict?如何仅使用 xmltodict 来做到这一点?

    import xmltodict
    import json

        A = xmltodict.parse(open('JeuxTestv2.gml').read(), attr_prefix='')

with open('test.json', "wt", encoding='utf-8', errors='ignore') as f:
   json.dump(dict, f, indent=4, sort_keys=True)

Json file: Json 文件:

{
        "product A": {
            "nature": "03",
            "product": "aaaa",
            "qualiteCategorisation": "01",
           }
        "xmlns:fn": "http://www.w3.org/2005/xpath-functions",
        "xmlns:xs": "http://www.w3.org/2001/XMLSchema"
    },

Something like this:像这样的东西:

In [3204]: d1 = { 
      ...:         "product A": { 
      ...:             "nature": "03", 
      ...:             "product": "aaaa", 
      ...:             "qualiteCategorisation": "01", 
      ...:            }, 
      ...:         "xmlns:fn": "http://www.w3.org/2005/xpath-functions", 
      ...:         "xmlns:xs": "http://www.w3.org/2001/XMLSchema" 
      ...:     } 

In [3210]: l = [x for x in d1.keys() if 'xmlns' in x]

In [3211]: for i in l: 
      ...:     d1.pop(i) 
      ...:                      

In [3212]: d1 
Out[3212]: 
{'product A': {'nature': '03',
  'product': 'aaaa',
  'qualiteCategorisation': '01'}}

Here is a one-liner using a dict-comprehension这是使用 dict-comprehension 的单行代码

In[1]: {k:v for k,v in d1.items() if "xmlns" not in k}
Out[2]: 
{'product A': {'nature': '03',
  'product': 'aaaa',
  'qualiteCategorisation': '01'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM