Learn Python – How to read CSV file in Python?- Basic and advance

The CSV file stands for a comma-separated values file. It is a type of undeniable textual content file the place the data is organized in the tabular form. It can include only the true textual content data. The textual records don’t want to be separated with the aid of the commas (,). There are additionally many separator characters such as tab (\t), colon(:), and semi-colon(;), which can be used as a separator. Let’s apprehend the following example.

Here, we have an example.txt file.

name, rollno, Department  
Peter Parker, 009001, Civil  
Tony Stark, 009002, Chemical  

Example –

# Read CSV file example  
# Importing the csv module  
import csv  
# open file by passing the file path.  
with open(r'C:\Users\DEVANSH SHARMA\Desktop\example.csv') as csv_file:  
    csv_read = csv.reader(csv_file, delimiter=',')  #Delimeter is comma   
    count_line = 0   
    # Iterate the file object or each row of the file  
    for row in csv_read:  
        if count_line == 0:  
            print(f'Column names are {", ".join(row)}')  
            count_line += 1  
        else:  
            print(f'\t{row[0]} roll number is:  {row[1]} and department is: {row[2]}.')  
            count_line += 1  
    print(f'Processed {count_line} lines.') # This line will print number of line fro the file  

Output:

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.

Explanation:

In the above code, we imported the csv module to read the example.csv file. To read the csv, we skip our file’s full direction in the open() method. We used the built-in characteristic csv.reader(), it takes two argument file object and delimiter. We initialized the count_line variable with 0 It counts the wide variety of line from the csv file.

Now, we iterated the each row of the csv file object. The data is again by putting off the delimiters. The first row returned which contains the column names.