【问题标题】:How to convert json keys into small letters? [duplicate]如何将json键转换为小写字母? [复制]
【发布时间】:2019-05-15 11:22:04
【问题描述】:

我需要将 json 键转换为小写字母,因为对象是从使用不一致的字母大小写收集的字符串构建的。我尝试了以下方法:

import json

alphabet = """{"My-Name": "ag=11", "Rule": "default"}"""
alphabetDict = json.loads(alphabet)
alphabetDictKeys = alphabetDict.keys()


for key, value in alphabetDict:
    smallalphabetDict[key.lower()] = value

smallalphabetDictKeys = smallalphabetDict.keys()
print("small keys:"+str(smallalphabetDictKeys))

# if statement using small case letters
if "my-name" in smallalphabetDictKeys:
    print("key found")
    print("value"+str(smallalphabetDict["my-name"])) 

但我收到此错误:

Traceback(最近一次调用最后一次):文件“test.py”,第 11 行,在 对于键,alphabetDict 中的值:ValueError:要解包的值太多(预期 2)

您能否纠正我的代码在转换中的错误?

【问题讨论】:

  • for key, value in alphabetDict.items(): ?

标签: python json python-3.x dictionary


【解决方案1】:

如果你想遍历字典,请使用 dict.items()

for key, value in alphabetDict.items():
    smallalphabetDict[key.lower()] = value

【讨论】:

    【解决方案2】:

    试试这个

    import json
    
    alphabet = """{"My-Name": "ag=11", "Rule": "default"}"""
    alphabetDict = json.loads(alphabet)
    alphabetDictKeys = alphabetDict.keys()
    smallalphabetDict={}
    
    for key, value in alphabetDict.items():
        smallalphabetDict[key.lower()] = value
    
    smallalphabetDictKeys = smallalphabetDict.keys()
    print("small keys:"+str(smallalphabetDictKeys))
    
    # if statement using small case letters
    if "my-name" in smallalphabetDictKeys:
        print("key found")
        print("value"+str(smallalphabetDict["my-name"]))
    

    【讨论】:

      【解决方案3】:

      代码中的一些问题

      • 您需要使用alphabetDict.items() 来迭代字典

      • if "my-name" in smallalphabetDict: 应该能够对键进行迭代,您不需要 smallalphabetDictKeys = smallalphabetDict.keys()

      • 您没有在代码中的任何地方使用alphabetDictKeys = alphabetDict.keys(),您可以删除该行

      修改后的代码如下所示

      import json
      
      alphabet = """{"My-Name": "ag=11", "Rule": "default"}"""
      alphabetDict = json.loads(alphabet)
      
      #Iterate of the keys and lower them
      smallalphabetDict = {}
      for key, value in alphabetDict.items():
          smallalphabetDict[key.lower()] = value
      
      # if statement using small case letters
      # Iterate on the keys again
      if "my-name" in smallalphabetDict:
          print("key found")
          print("value"+str(smallalphabetDict["my-name"]))
      

      输出将是

      key found
      valueag=11
      

      您还可以通过字典理解来简化代码,并使用dict.get 来获取您的项目

      import json
      
      alphabet = """{"My-Name": "ag=11", "Rule": "default"}"""
      alphabetDict = json.loads(alphabet)
      
      #Iterate of the keys and lower them via dictionary comprehension
      smallalphabetDict = {key.lower(): value for key, value in alphabetDict.items()}
      
      #Use dict.get to get the value for the key
      print(smallalphabetDict.get("my-name"))
      

      输出将是

      ag=11
      

      【讨论】:

        【解决方案4】:

        检查此代码

        import json
        
        alphabet = """{"My-Name": "ag=11", "Rule": "default"}"""
        alphabetDict = json.loads(alphabet)
        alphabetDictKeys = alphabetDict.keys()
        
        smallalphabetDict = {}
        for key in alphabetDict:
            value = alphabetDict[key]
            smallalphabetDict[key.lower()] = value
        
        smallalphabetDictKeys = smallalphabetDict.keys()
        print("small keys:"+str(smallalphabetDictKeys))
        
        # if statement using small case letters
        if "my-name" in smallalphabetDictKeys:
            print("key found")
            print("value"+str(smallalphabetDict["my-name"])) 
        
        

        【讨论】:

          猜你喜欢
          • 2013-02-04
          • 1970-01-01
          • 2015-08-07
          • 2011-12-13
          • 2015-07-28
          • 1970-01-01
          • 2013-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多