1,Ravi,20,30,40,120
2,Raju,30,40,50,130
3,Alex,40,50,60,140
4,Ronn,50,60,70,150
#create a dictionary with student name and marks.
#Use student name as key
#User has to enter all data.
fob=open('student.csv','a')
data=''
for i in range(1,5):
n=input("Student Name : ")
a=input("\n Enter maths marks: ")
b=input("Enter physics marks: ")
c=input("Enter Chemistry marks: ")
p=input("Enter no of days present: ")
data=str(i)+','+n+','+a+','+b+','+c+','+p+'\n'
fob.write(data)
fob.close()
creating dictionary and showing data
fob=open('student.csv','r')
my_dict={}
for rec in fob:
n=rec.split(',')
print(n)
my_dict.update({n[0]:[n[1],n[2],n[3],n[4],n[-1].rstrip('\n')]})
fob.close()
print(my_dict)
print(my_dict['2'])
print(my_dict['2'][1])
Read student details from a CSV file and prepare a dictionaryfob=open('student.csv','r')
my_dict={}
for rec in fob:
n=rec.split(',')
print(n)
my_dict.update({n[0]:[n[1],n[2],n[3],n[4],n[-1].rstrip('\n')]})
fob.close()
print(my_dict)
print(my_dict['2'])
print(my_dict['2'][1])
Create one file student1.csv using dictionary data. We used append mode for file write.
fob=open('student1.csv','a')
data=''
for i in my_dict:
data=i +','+ my_dict[i][0] +','+ my_dict[i][1] +',' +my_dict[i][2]
data = data + ','+my_dict[i][3] + ','+ my_dict[i][4] + '\n'
fob.write(data)
fob.close()
Create one more csv file after reducing mark in Physics. We used int() to convert string to integer and then used str() to again convert integer to string after reducing the mark.
fob=open('student2.csv','a')
data=''
for i in my_dict:
data=i +','+ my_dict[i][0] +','+ my_dict[i][1] +',' +str(int(my_dict[i][2])-2)
data = data + ','+my_dict[i][3] + ','+ my_dict[i][4] + '\n'
fob.write(data)
fob.close()
Download zip file to check .ipynb file ( file_dicitionary_csv_data.ipynb )
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.