CSV Files
CSV Files
CSV Files
Each line of the file is a data record. Each record consists of one or more
fields, separated by commas.
For working CSV files in Python, there is an inbuilt module called
CSV.
It is important to know to work with CSV because we mostly rely on CSV data
in our day-to-day lives as data scientists.
The so-called CSV (Comma Separated Values) format is the most common
import and export format for spreadsheets and databases. The lack
of a well-defined standard means that subtle differences often exist in the
data produced and consumed by different applications. These differences
can make it annoying to process CSV files from multiple sources. Still, while
the delimiters and quoting characters vary, the overall format is similar
enough that it is possible to write a single module which can efficiently
manipulate such data, hiding the details of reading and writing the data from
the programmer.
The csv module’s reader and writer objects read and write
sequences.
We have a file named “Salary_Data.csv.” The first line of a CSV file is the
header and contains the names of the fields/features.
After the header, each line of the file is an observation/a record. The values
of a record are separated by “comma.”
Excel Vs CSV
Excel CSV
It stores data along with formatting It only stores data
and formulae
Extension is .xls or xlsx Extension is .csv
File cannot be opened with other File can be opened with other editors
editors like notepad like notepad
Consumes more memory Consumes less memory
import scv
In Python, the csv.reader() module is used to read the csv file. It takes each
row of the file and makes a list of all the columns.
Example 1: (Method 1)
import csv
file = open("C:\\Users\\Student\\Desktop\\Sample100.csv","r")
print(file.read())
Output:
Example 2: (Method 2)
import csv
# Return a reader object which will iterate over lines in the given csvfile
for line in csv_read:
print(line)
Note: csv.reader return a reader object which will iterate over lines in the
given csvfile
Output:
import csv
print(row)
Instead of printing the data we can find out the number rows in a particular
data set.
Example:
import csv
csv_read=csv.reader(f1)
pass
Output:
We can also write any new and existing CSV files in Python by using the
csv.writer() module. It is similar to the csv.reader() module and also has two
methods, i.e., writer function or the Dict Writer class.
Example:
import csv
csv_write = csv.writer(f1)
Output:
Note:
To add data (append) in already existing file open file in “a” mode.
Close the file
.close() method is used to close the opened file. Once it is closed, we cannot
perform any operations on it.
file.close()
Implementing the above code using with() statement:
Syntax: with open(filename, mode) as alias_filename:
Modes:
CSV Advantages
CSV Disadvantages
Problems with importing CSV into SQL (no distinction between NULL
and quotes)