I have a csv data set that opened used the DictReader in python and I would like the separate the keys and values.
import csv
with open ('student_marks.txt') as csv_file:
csv_reader = csv.DictReader(csv_file)
for keys in csv_reader:
print(keys)
I have this output and I wish to separate the keys and values in the out put
{'Student ID': '4665178', 'Homework Total (20%)': '95.9', 'Project 1 (10%)': '91.9', 'Midterm (20%)': '91.2', 'Project 2 (10%)': '90.9', 'Final (40%)': '99'}
{'Student ID': '4665187', 'Homework Total (20%)': '83.1', 'Project 1 (10%)': '83', 'Midterm (20%)': '75.7', 'Project 2 (10%)': '92', 'Final (40%)': '78.6'}
{'Student ID': '4665203', 'Homework Total (20%)': '89.9', 'Project 1 (10%)': '73.6', 'Midterm (20%)': '98.9', 'Project 2 (10%)': '84.1', 'Final (40%)': '95.2'}
{'Student ID': '4665219', 'Homework Total (20%)': '81.3', 'Project 1 (10%)': '88.5', 'Midterm (20%)': '84.8', 'Project 2 (10%)': '81.2', 'Final (40%)': '91.1'}
CodePudding user response:
You can use the built-in method like this:
import csv
with open('student_marks.txt') as csv_file:
csv_reader = csv.DictReader(csv_file)
for i in csv_reader:
print(list(i.keys()))
print(list(i.values()))
i.keys
method will always return the same header or first line of your csv file in each iteration.