WHAT IS CSV?

A comma separated values (CSV) file contains different values separated by a delimiter, which acts as a database table or an intermediate form of a database table. In other words, a CSV file is a set of database rows and columns stored in a text file such that the rows are separated by a new line while the columns are separated by a semicolon or a comma. A CSV file is primarily used to transport data between two databases of different formats through a computer program.


ADVANTAGES OF CSV:-

The advantage of using CSV file format for data exchange is that the CSV file is relatively easy to process by any application and data extraction can be achieved with the help of a simple program. In the earlier years when database technologies were still in their infancy, the CSV was the most standard portable format. For the most part, a CSV file would only be used for a relatively simple application, while XML would be used for a transfer involving more complexity.


ILLUSTRATIONS:-

The following sequence illustrates a typical CSV file:

John Smith, 50, $5000, New York

David Benz, 36, $10000, Miami

Note that each the line of text corresponds to a row in the database table. The various columns are represented with comma. Generally, the following are true:

  • The leading and trailing spaces are generally ignored when conversion is being made from CSV to a typical database.
  • Double quotes are used to delimit embedded commas.
  • A field that contains double quotes is surrounded by double quotes and the embedded double quotes in the field are also surrounded by an additional pair of double quotes.
  • A field that has embedded line breaks is also surrounded by double quotes.
  • The fields at the top row indicate the column names of the target table into which the CSV file will be converted.


What is a CSV looks like:-

If you open up a CSV file in a text editor it would look something like:

A,B,C
1,2,3
4,"5,3",6

Here there are 3 rows each of 3 columns. Notice how the second column in the last line is "quoted" because the content of that value actually contains a "," character. Without the quotes this character would be interpreted as a column separator. To avoid this confusion we put quotes around the whole value. The result is that we have 3 rows each of 3 columns (Note a CSV file does not have to have the same number of columns in each row).