如何在嵌套的JSON输出中获取值?

vsikbqxv  于 2023-10-08  发布在  其他
关注(0)|答案(2)|浏览(138)

我是Python新手。我有一个JSON输出,并从中获取特定的值。有人能帮我得到具体的值吗

{"ABCDE": [{"Title": "Country Manager", "Name": "xxx yyy", "Office": "123-456-1234", "Cell": "xxx-xxx-xxxx", "Email": "[email protected]"}, {"Title": "Regional Manager", "Name": "abc redsbj", "Office": "yyy-yyy-yyyy", "Cell": "eee-eee-eeee", "Email": "[email protected]"}, {"Title": "General Manager", "Name": "Xyz whatever", "Office": "xxx-xxx-xxxx", "Cell": "xxx-xxx-xxxx", "Email": "[email protected]"}, {"Title": "General Manager - Regional", "Name": "whatever name", "Office": "www-www-wwww", "Cell": "rrr-rrr-rrrr", "Email": "[email protected]"}], "VWXYZ": [{"Title": "Country Manager", "Name": "nnn nnnnnnn", "Office": "123-123-1234", "Work Cell": NaN, "Email": [email protected]}, {"Title": "Regional Manager", "Name": "jane doe", "Office": "1234567890", "Work Cell": "1231231234", "Email": "[email protected]"}, {"Title": "General Manager", "Name": "Doe Jane", "Office": "1232343456", "Work Cell": "9098981234", "Email": "[email protected]"}]}

预期输出

'ABCDE':'xxx-xxx-xxxx','单元格':'xxx-xxx-xxxx','电子邮件':“email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)”],“VWXYZ”:“General Manager”:“Doe Jane”,“”:'1232343456','工作单元':'9098981234','电子邮件':' email protected(https://stackoverflow.com/cdn-cgi/l/email-protection) ']}

with open(htmlFilePath, 'r', encoding='utf-8') as file:
    siteCode = str(file_name)[:3]       
    
    tables = pd.read_html(file)       
    df = tables[1]        
    if df.columns[0] != "Title":
        df.columns = df.iloc[0]            
        df = df[1:]            
        df = df.reset_index(drop=True)        
    atoDataDict[key] = df.to_dict("records")

jsonOutput = json.dumps(atoDataDict)

bakd9h0s

bakd9h0s1#

如果你纠正你的输入和输出,我相信你正在寻找这样的事情:

data_out = {}
for key, value in data_in.items():
    data_out[key] = []
    for item in value:
        if not item["Title"] == "General Manager":
            continue

        item["General Manager"] = item["Name"]
        del item["Title"]
        del item["Name"]
        data_out[key].append(item)
u59ebvdq

u59ebvdq2#

你可以重新格式化你的数据data = { 'ABCDE':{“总经理”:'Xyz转换器','转换器':'xxx-xxx-xxxx','单元格':'xxx-xxx-xxxx','电子邮件':' email protected(https://stackoverflow.com/cdn-cgi/l/email-protection) ' },'VWXYZ':{“总经理”:“无名氏简”,“女”:'1232343456','工作单元':'9098981234','电子邮件':'email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)' } },然后您可以访问所需的特定值,例如manager_abcde = data 'ABCDE']'General Manager']

相关问题