A csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. The csv file opens into the excel sheet, and the rows and columns data define the standard format.
The CSV module work is to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
We can also write any new and existing csv files in Python by using csv.writer() module. It is similar to csv.reader() module and also has two methods, i.e. writer function or the Dict Writer class.
It presents two functions, i.e., writerow() and writerows(). The writerow() function only write one row and the writerows() function write more than one row.
It is defined as a construct that allows you to create, store, and re-use various formatting parameters. It supports the several attributes; the most frequently used are:
Let's write following data to a CSV File.
data = [{'Rank': 'B', 'first_name': 'Parker', 'last_name': 'Brian'}, {'Rank': 'A', 'first_name': 'Smith', 'last_name': 'Rodriguez'}, {'Rank': 'C', 'first_name': 'Tom', 'last_name': 'smith'}, {'Rank': 'B', 'first_name': 'Jane', 'last_name': 'Oscar'}, {'Rank': 'A', 'first_name': 'Alex', 'last_name': 'Tim'}]
import csv with open('Python.csv', 'w') as csvfile: fieldnames = ['first_name', 'last_name', 'Rank'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'Rank': 'B', 'first_name': 'Parker', 'last_name': 'Brian'}) writer.writerow({'Rank': 'A', 'first_name': 'Smith', 'last_name': 'Rodriguez'}) writer.writerow({'Rank': 'B', 'first_name': 'Jane', 'last_name': 'Oscar'}) writer.writerow({'Rank': 'B', 'first_name': 'Jane', 'last_name': 'Loive'}) print("Writing complete")
It returns the file named as 'Python.csv' that contains the following data:
first_name,last_name,Rank Parker,Brian,B Smith,Rodriguez,A Jane,Oscar,B Jane,Loive,B
We can also use the class DictWriter to write the csv file directly into a dictionary.
A file named as python.txt contains following data:
Parker,Accounting,November Smith,IT,October
import csv with open('python.txt', mode='w') as csv_file: fieldnames = ['emp_name', 'dept', 'birth_month'] writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() writer.writerow({'emp_name': 'Parker', 'dept': 'Accounting', 'birth_month': 'November'}) writer.writerow({'emp_name': 'Smith', 'dept': 'IT', 'birth_month': 'October'})
Pandas is defined as an open source library which is built on the top of NumPy library. It provides fast analysis, data cleaning and preparation of the data for the user.
It is as easy as reading the csv file using pandas. You need to create the DataFrame, which is a two-dimensional, heterogeneous tabular data structure and consists of three main components- data, columns, and rows.
Here, we take a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.
Name,Hire Date,Salary,Leaves Remaining John Idle,08/15/14,50000.00,10 Smith Gilliam,04/07/15,65000.00,8 Parker Chapman,02/21/14,45000.00,10 Jones Palin,10/14/13,70000.00,3 Terry Gilliam,07/22/14,48000.00,7 Michael Palin,06/28/13,66000.00,8
import pandas df = pandas.read_csv('hrdata.csv', index_col='Employee', parse_dates=['Hired'], header=0, names=['Employee', 'Hired', 'Salary', 'Sick Days']) df.to_csv('hrdata_modified.csv')