微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用平面数据表中的嵌套记录构建JSON文件?

如何解决如何使用平面数据表中的嵌套记录构建JSON文件?

这是一个可行的解决方案,可以创建所需的JSON格式。首先,我将数据帧按适当的列进行分组,然后为每个列标题/记录对创建字典(而不丢失数据顺序),而是将它们创建为元组列表,然后将列表转换为有序字典。为其他所有分组的两个列创建了另一个有序字典。为了使JSON转换产生正确的格式,列表和有序的dict之间必须进行精确的分层。另请注意,转储为JSON时,必须将sort_keys设置为false,否则所有的Ordered Dicts都将重新排列为字母顺序。

import pandas
import json
from collections import OrderedDict

inputExcel = 'E:\\teams.xlsx'
exportJson = 'E:\\teams.json'

data = pandas.read_excel(inputExcel, sheetname = 'SCAT Teams', encoding = 'utf8')

# This creates a tuple of column headings for later use matching them with column data
cols = []
columnList = list(data[0:])
for col in columnList:
    cols.append(str(col))
columnList = tuple(cols)

#This groups the dataframe by the 'teamname' and 'members' columns
grouped = data.groupby(['teamname', 'members']).first()

#This creates a reference to the index level of the groups
groupnames = data.groupby(["teamname", "members"]).grouper.levels
tm = (groupnames[0])

#Create a list to add team records to at the end of the first 'for' loop
teamsList = []

for teamN in tm:
    teamN = int(teamN)  #added this in to prevent TypeError: 1 is not JSON serializable
    tempList = []   #Create an temporary list to add each record to
    for index, row in grouped.iterrows():
        daTarow = row
        if index[0] == teamN:  #Select the record in each row of the grouped dataframe if its index matches the team number

            #In order to have the JSON records come out in the same order, I had to first create a list of tuples, then convert to and Ordered Dict
            rowDict = ([(columnList[2], daTarow[0]), (columnList[3], daTarow[1]), (columnList[4], daTarow[2]), (columnList[5], daTarow[3]), (columnList[6], daTarow[4]), (columnList[7], daTarow[5])])
            rowDict = OrderedDict(rowDict)
            tempList.append(rowDict)
    #Create another Ordered Dict to keep 'teamname' and the list of members from the temporary list sorted
    t = ([('teamname', str(teamN)), ('members', tempList)])
    t= OrderedDict(t)

    #Append the Ordered Dict to the emepty list of teams created earlier
    ListX = t
    teamsList.append(ListX)


#Create a final dictionary with a single item: the list of teams
teams = {"teams":teamsList}

#Dump to JSON format
formattedJson = json.dumps(teams, indent = 1, sort_keys = False) #sort_keys MUST be set to False, or all dictionaries will be alphebetized
formattedJson = formattedJson.replace("NaN", '"NULL"') #"NaN" is the NULL format in pandas dataframes - must be replaced with "NULL" to be a valid JSON file
print formattedJson

#Export to JSON file
parsed = open(exportJson, "w")
parsed.write(formattedJson)

print"\n\nExport to JSON Complete"

解决方法

我正在寻找一种Python技术,从pandas数据框中的平面表构建嵌套的JSON文件。例如,一个大熊猫数据框表如:

teamname  member firstname lastname  orgname         phone        mobile
0        1       0      John      Doe     Anon  916-555-1234                 
1        1       1      Jane      Doe     Anon  916-555-4321  916-555-7890   
2        2       0    Mickey    Moose  Moosers  916-555-0000  916-555-1111   
3        2       1     Minny    Moose  Moosers  916-555-2222

将其导出并导出为如下所示的JSON:

{
"teams": [
{
"teamname": "1","members": [
  {
    "firstname": "John","lastname": "Doe","orgname": "Anon","phone": "916-555-1234","mobile": "",},{
    "firstname": "Jane","phone": "916-555-4321","mobile": "916-555-7890",}
]
},{
"teamname": "2","members": [
  {
    "firstname": "Mickey","lastname": "Moose","orgname": "Moosers","phone": "916-555-0000","mobile": "916-555-1111",{
    "firstname": "Minny","phone": "916-555-2222",}
]
}       
]

}

我尝试通过创建一个dict字典并将其转储到JSON来做到这一点。这是我当前的代码:

data = pandas.read_excel(inputExcel,sheetname = 'SCAT Teams',encoding = 'utf8')
memberDictTuple = []

for index,row in data.iterrows():
    dataRow = row
    rowDict = dict(zip(columnList[2:],dataRow[2:]))

    teamRowDict = {columnList[0]:int(dataRow[0])}

    memberId = tuple(row[1:2])
    memberId = memberId[0]

    teamName = tuple(row[0:1])
    teamName = teamName[0]

    memberDict1 = {int(memberId):rowDict}
    memberDict2 = {int(teamName):memberDict1}

    memberDictTuple.append(memberDict2)

memberDictTuple = tuple(memberDictTuple)
formattedJson = json.dumps(memberDictTuple,indent = 4,sort_keys = True)
print formattedJson

这将产生以下输出。每个项目都嵌套在“团队名称”
1或2下的正确级别上,但是如果记录具有相同的团队名称,则应将它们嵌套在一起。如何解决此问题,使组名1和组名2各自嵌套2条记录?

[
    {
        "1": {
            "0": {
                "email": "john.doe@wildlife.net","firstname": "John","mobile": "none","phone": "916-555-1234"
            }
        }
    },{
        "1": {
            "1": {
                "email": "jane.doe@wildlife.net","firstname": "Jane","phone": "916-555-4321"
            }
        }
    },{
        "2": {
            "0": {
                "email": "mickey.moose@wildlife.net","firstname": "Mickey","phone": "916-555-0000"
            }
        }
    },{
        "2": {
            "1": {
                "email": "minny.moose@wildlife.net","firstname": "Minny","phone": "916-555-2222"
            }
        }
    }
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。