from collections import namedtuple
Data=namedtuple('Data','name number age cgpa')#best to keep all data types as string
def prettyprint(X,end="\n"):
s="{0},{1},{2},{3}".format(X.name,X.number,X.age,X.cgpa)
print(s,end=end)
file_name=r"""C:\Users\DELL\OneDrive\Desktop\Students Data.txt"""
file=open(file_name,'r')
List=[]
for i in file.readlines():
temp=[j.rstrip().lstrip() for j in i.split(',')]
List.append(tuple(temp))
Students=list(map(lambda x:Data(*x),List))
Sort_cgpa=sorted(Students,key=lambda x:(float(x.cgpa),x.name,int(x.number[-4:]),int(x.age)),reverse=True)
for out in Sort_cgpa:
prettyprint(out)
Students Data.txt contains the data of the students in Name, Number, Age, CGPA. Thing is I will be using age as int and cgpa as float variables I have no issues to use all the data types as string and convert the data type to needed any time I want to. So, is the best way to use namedtuple as a single datatype like string.
Note that while I can change
temp_new=[[i[0],i[1],int(i[2]),float(i[3])] for i in temp]
List.append(temp_new)
My question isn't specific to this code I want to know is there a way to change init(self) in the so that I can use self.age=int(self.age) or it is best to use all the datatypes as string