【问题标题】:Change dictionary value in list of dictionaries更改字典列表中的字典值
【发布时间】:2019-10-19 18:10:29
【问题描述】:
{
  "__class": "Tape",
  "Clips": [
    {
      "__class": "MotionClip",
      "Id": 4280596098,
      "TrackId": 4094799440,
      "IsActive": 1,
      "StartTime": 180,
      "Duration": 48,
      "ClassifierPath": "world\/maps\/error\/timeline\/moves\/error_intro_walk.msm",
      "GoldMove": 0,
      "CoachId": 0,
      "MoveType": 0,
   {
      "__class": "MotionClip",
      "Id": 2393481294,
      "TrackId": 4094799440,
      "IsActive": 1,
      "StartTime": 372,
      "Duration": 48,
      "ClassifierPath": "world\/maps\/error\/timeline\/moves\/error_ve_opening.msm",
      "GoldMove": 0,
      "CoachId": 0,
      "MoveType": 0,
...

这是一个 json 文件的 sn-p,我想在其中更改数字的显示方式。例如,我想将 1 写为 00000001,将 48 写为 00000048,将 0 写为 00000000。所以整个 json 文件中的所有数字都按我之前所说的那样写。它们可以写成字符串。 我不知道如何开始,因为我有字典列表。

【问题讨论】:

    标签: json python-3.x list dictionary


    【解决方案1】:

    假设您的数据实际上是这样的 -

    main_dict = {
      "__class": "Tape",
      "Clips": [
        {
          "__class": "MotionClip",
          "Id": 4280596098,
          "TrackId": 4094799440,
          "IsActive": 1,
          "StartTime": 180,
          "Duration": 48,
          "ClassifierPath": "world\/maps\/error\/timeline\/moves\/error_intro_walk.msm",
          "GoldMove": 0,
          "CoachId": 0,
          "MoveType": 0,
       },
       {
          "__class": "MotionClip",
          "Id": 2393481294,
          "TrackId": 4094799440,
          "IsActive": 1,
          "StartTime": 372,
          "Duration": 48,
          "ClassifierPath": "world\/maps\/error\/timeline\/moves\/error_ve_opening.msm",
          "GoldMove": 0,
          "CoachId": 0,
          "MoveType": 0,
       }
      ]
    }  
    
    

    您可以执行以下操作 -

    #Accessing a specific key in a dict, in this case Clips (list of dicts)
    list_dicts = main_dict.get('Clips')  
    
    #Function to fix/edit specific values in the list of dict based on what key they belong to
    def fix_value(k,v):
    
        if k=='Duration':
            return '00000'+str(v)
    
        #Add more elseif in between with various things you wanna handle 
    
        else:
            return v
    
    #Iterating over each key, value in each dict in list of dicts and applying above function on the values
    
    [{k:fix_value(k,v) for k,v in i.items()} for i in list_dicts] 
    
    

    结果 -

    [{'__class': 'MotionClip',
      'Id': 4280596098,
      'TrackId': 4094799440,
      'IsActive': 1,
      'StartTime': 180,
      'Duration': '0000048',
      'ClassifierPath': 'world\\/maps\\/error\\/timeline\\/moves\\/error_intro_walk.msm',
      'GoldMove': 0,
      'CoachId': 0,
      'MoveType': 0},
     {'__class': 'MotionClip',
      'Id': 2393481294,
      'TrackId': 4094799440,
      'IsActive': 1,
      'StartTime': 372,
      'Duration': '0000048',
      'ClassifierPath': 'world\\/maps\\/error\\/timeline\\/moves\\/error_ve_opening.msm',
      'GoldMove': 0,
      'CoachId': 0,
      'MoveType': 0}]
    
    

    【讨论】:

    • data=[{k:fix_value(k,v) for k,v in i.items()} for i in list_dicts] s = json.dumps(data, indent=4) print(re.sub('(\d+)', lambda i: hex(int(i.group(0))),s)) 最后两行应该将所有数字转换为十六进制。他们有,但我没有得到我想要的格式。它们是打印出来的,因为我什至没有应用您的解决方案。我只使用最后两行代码就得到了相同的结果。
    • [{"__class": "MotionClip", "Id": 0xff24b682, "TrackId": 0xf411ae50, "IsActive": "0x1", "StartTime": 0xb4, "Duration": 0x30, "ClassifierPath": "world/maps/error/timeline/moves/error_intro_walk.msm", "GoldMove": 0x0, "CoachId": 0x0, "MoveType": 0x0}, {"__class": "MotionClip", "Id": 0x8ea9a04e, "TrackId": 0xf411ae50, "IsActive": "0x1", "StartTime": 0x174, "Duration": 0x30, "ClassifierPath": "world/maps/error/timeline/moves/error_ve_opening.msm", "GoldMove": 0x0, "CoachId": 0x0, "MoveType": 0x0}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多