In: Computer Science
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
Hi,
So I ran this code to help you understand whats happening. I have taken a diabetes dataset from kaggle for reference.
It ended up printing 2 columns:
First column: index number
Second column: the column name
Heres the code:
import csv
filename = "diabetes.csv"
with open(filename) as file:
data_from_file = csv.reader(file)
header_row = next(data_from_file)
for index,column_header in enumerate(header_row):
print(index,column_header)
Reference to the dataset:
The output:
Therefore as we can see, 2 columns are printed.
This was a fun question to solve and if there's any doubt please let me know in the comment section.
Thanks and All the Best :D