【问题标题】:How can I define a structure of a json to transform it to csv如何定义 json 的结构以将其转换为 csv
【发布时间】:2021-08-03 09:55:51
【问题描述】:

我的 json 结构如下:

    { 
 "data": [
   {
      "groups": {
         "data": [
               {
               "group_name": "Wedding planning - places and bands (and others) to recommend!",
               "date_joined": "2009-03-12 01:01:08.677427"
               },
               {
               "group_name": "Harry Potter and the Deathly Hollows",
               "date_joined": "2009-01-15 01:38:06.822220"
               },
               {
               "group_name": "Xbox , Playstation, Wii - console fans",
               "date_joined": "2010-04-02 04:02:58.078934"
               }
         ]
      },
      "id": "0"
   },
   {
      "groups": {
         "data": [
               {
               "group_name": "Lost&Found  (Strzegom)",
               "date_joined": "2010-02-01 14:13:34.551920"
               },
               {
               "group_name": "Tennis, Squash, Badminton, table tennis - looking for sparring partner (Strzegom)",
               "date_joined": "2008-09-24 17:29:43.356992"
               }
        ]
      },
      "id": "1"
   }
 ]
}

如何解析这种形式的 json?我应该尝试建立一个类似这种格式的课程吗?我想要的输出是一个 csv,其中索引是一个“id”,在第一列中我有最近使用的组,在第二列中是最近使用的第二组,依此类推。

意思是这样的结果是:

      most recent                               second most recent
0     Xbox , Playstation, Wii - console fans    Wedding planning - places and bands (and others) to recommend!
1     Lost&Found  (Strzegom)                    Tennis, Squash, Badminton, table tennis - looking for sparring partner (Strzegom)

【问题讨论】:

  • 如果是一次性的,那么一门课就显得多余了。只需在每个排序组中循环 data 中的项目并在两次迭代后停止。
  • 切线地,您想要的结果看起来相当令人反感。你真正想解决什么问题?
  • 我想将文本连同我在另一个 csv 中的另一个文本列一起提供给神经网络,所以我认为最好将它放在 csv 中?为什么看起来不好吃?
  • 如果您不打算为此使用 Python,jq -r '.data[].groups.data|=sort_by(.date_joined)|.data[]|[.id, .groups.data[0].group_name, .groups.data[1].group_name]|@csv' 也很接近...请参阅 stackoverflow.com/questions/35540294/… 了解如何反转排序。
  • 有两个文本栏看起来不自然;也许改为添加一个优先级,例如 0,0,Xbox; 0,1,婚礼策划; 1,0,失物招领处; 1,1、网球、壁球

标签: python json dataframe


【解决方案1】:

解决方案可能是这样的:

data = json.load(f)
result = []
# it's max element in there for each id. Helping how many group_name here for this example [3,2]
max_element_group_name = [len(data['data'][i]['groups']['data']) for i in range(len(data['data']))]
max_element_group_name.sort()

for i in range(len(data['data'])):
    # get id for each groups
    id = data['data'][i]['id']
    # sort data_joined in groups
    sorted_groups_by_date = sorted(data['data'][i]['groups']['data'],key=lambda x : time.strptime(x['date_joined'],'%Y-%m-%d %H:%M:%S.%f'),reverse=True)
    # get groups name using minumum value in max_element_group_name for this example [2] 
    group_names = [sorted_groups_by_date[j]['group_name'] for j in range(max_element_group_name[0])]
    # add result list with id
    result.append([id]+group_names)

# create df for list
df = pd.DataFrame(result, columns = ['id','most recent', 'second most recent'])
# it could be better. 

【讨论】:

    猜你喜欢
    • 2017-10-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2020-01-06
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多