Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort data from JSON file
#1
Hi there! Have the following task:

***
data.json - file with data about ancient mathematicians.
1. Write function, which reads data from the file. Function parameter is the name of the file.
2. Write function which sorts data by surname in the field 'name' (for those who have it).
For example, in case of Rene Descartes surname is Descartes, for Pierre de Fermat it is Fermat etc.
If there is no surname, use name, for example Euclid.
***

It seems I've coped with the first part, but a bit confused by the second task. As the field name can contain only name, a name and surname, or a name, such word as "de" and surname. But we need to sort data by surname only. Not sure how to code it. Here's what I've done so far:

import json
def read_json(data):
    with open(data, "r", encoding='utf-8') as file:
        result = json.load(file)
    return result

def sort_by_surname(data):
    surname = 
# suppose here I should convert "name" field to a string, then use split to separate words, and make it use the latter word in case there are 2 or 3 words - sounds a bit complicated for me. Is there another way? Or is my version correct. Would be great if you could help me **huh** 
    sort_surname = sorted(data, key=abs(surname))
    print(sort_surname)
Pls help Huh Thanks in advance!
buran write Jan-04-2021, 08:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Sort data from JSON file - by Dummy_in_programming - Jan-04-2021, 05:57 PM
RE: Sort data from JSON file - by bowlofred - Jan-04-2021, 06:08 PM
RE: Sort data from JSON file - by deanhystad - Jan-04-2021, 06:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 268 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 791 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,590 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,194 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,662 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,307 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Read nested data from JSON - Getting an error marlonbown 5 1,423 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,122 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  How to sort .csv file test log which item first fail and paint color SamLiu 24 5,100 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Writing to json file ebolisa 1 1,032 Jul-17-2022, 04:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020