CSV files in python

CSV files:

CSV stands for comma separated values file. These are usually normal text files in the tabular data format style. In these files we use comma as delimiter between columns data. See below file format

Column1, column 2,column 3,....

Data1  ,Data2, Data3,.....

That is how it looks like.

We can use other delimiters also like | , colon :,semi colon ; etc.

Use of CSV files.

CSV files are used export data from application to other application file. We can also use to import data from data application programs and can further use it.

Example is we can take data from database file and export it to spreadsheet programs like Excel.

 

Reading CSV files:

Reading of CSV file is done using reader object. CSV file is opened with open function in text mode which is the default mode. See program code below.

import csv

With open('mydata.txt','r') as file:

   csv_reader=csv.reader(file, delimiter=',')

   for row in csv_reader:

      print(row[0]+' , '+row[1]+' , '+row[2])