How to read CSV file in Python?
The CSV file stands for a comma-separated values file. It is a type of plain text file where the information is organized in the tabular form. It can contain only the actual text data. The textual data don't need to be separated by the commas (,). There are also many separator characters such as tab (\t), colon(:), and semi-colon(;), which can be used as a separator. Let's understand the following example.
Here, we have an example.txt file.
Column names are name, rollnu, Department Peter Parker roll number is: 009001 and department is: Civil. Tony Stark roll number is: 009002 and department is: Chemical. Processed 3 lines.
In the above code, we imported the csv module to read the example.csv file. To read the csv, we pass our file's full path in the open() method. We used the built-in function csv.reader(), it takes two argument file object and delimiter. We initialized the count_line variable with 0. It counts the number of line from the csv file.
Now, we iterated the each row of the csv file object. The data is returned by removing the delimiters. The first row returned which contains the column names.