Page 141 - PYTHON-12
P. 141
In student.csv (notepad) file, first line is the header and remaining lines are the data/records. The
fields are separated by comma, or we may say the separator character. In general, the separator
character is called a delimiter, and the comma is not the only one used. Other popular delimiters
include the tab (\t), colon (:) and semi-colon (;) characters.
Practical Implementation–5
Write a program to read the contents of “student.csv” file.
Explanation:
As seen from the above output, every record is stored in reader object in the form of a List. In the
above code, we first open the CSV file in READ mode. The file object is named as f. The file object
is converted to csv.reader object. We save the csv.reader object as csv_reader. The reader object is
Computer Science with Python–XII 4.12 values. Thus, all the records are displayed as lists separated by comma.
used to read records as lists from a csv file. Now, we iterate through all the rows using a for loop.
When we try to print each row, one can find that row is nothing but a list containing all the field
In the next implementation, we will count the number of records present inside the student.csv
file.