Jun-06-2020, 04:08 PM
import json
with open("users.json") as datafile:
data = json.load(datafile) # loaded json file data into python string
data_with_id = [] #For addinng data with new id numbers to be used for grouping
def newdict(): #function for adding items to new empty dictionary
n=1000 # variable for adding data with ID numbers in new dictionary
for items in data:
data_with_id.append({"idnum":n,"name":items["first_name"],"gender":items["gender"],"email":items["email"]})
n = n+1
newdict ()
print("\nUsers with alloted ID are as below--\n")
for users in data_with_id:
print("ID number",":" ,users["idnum"],"Name",":",users["name"],"gender",":",users["gender"],"E-Mail address",":",users["email"])
id_num_list = [] #For only ID numbers added as n in newdict
for users in data_with_id:
id_num_list.append(str(users["idnum"]))
print("\n ID numbers are : ",id_num_list)
def create_new_group(selected_ids_list):
new_group = []
for n in data_with_id:
for m in selected_ids_list:
if m in str(n["idnum"]):
new_group.append({n["name"],n["gender"],n["email"]})
with open("write_it.json","w") as writefile:
writefile.write(json.dumps(new_group))
print("\n File is saved")
def selection_for_grouping():
x = input("\nselect ID numbers by comma seperated from list to create a group : ")
selected_ids_list = x.split(",")
print("\nselected IDs are : ",selected_ids_list)
for selected in selected_ids_list:
if selected in id_num_list:
continue
if selected not in id_num_list:
print("ID does not exists :",selected)
create_new_group(selected_ids_list)
selection_for_grouping()
print("Great its done")Error:Traceback (most recent call last):
File "C:\Users\Vinay.Prajapati\Desktop\Python Programs\reading json file - users - basic.py", line 51, in <module>
selection_for_grouping()
File "C:\Users\Vinay.Prajapati\Desktop\Python Programs\reading json file - users - basic.py", line 49, in selection_for_grouping
create_new_group(selected_ids_list)
File "C:\Users\Vinay.Prajapati\Desktop\Python Programs\reading json file - users - basic.py", line 36, in create_new_group
writefile.write(json.dumps(new_group))
File "C:\Python\Python Program\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python\Python Program\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python\Python Program\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Python\Python Program\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializableThe issue is with saving the new_group (selected ids by user) is not being saved in json showing error as above. the reason is its type is set instead of dictionary. so i need help what i can do here to solve this.I also want it to save as group_name as per the name input by user string
Please note I am a bigginer, learning python and doing practice project by self.
I have recently learnt basics of python.
