Python CSV

Handling CSV files in Python

What is CSV

CSV stands for “Comma Separated Values.” It is the simplest form of storing data in tabular form as plain text. 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. Consider this example .

CSV File Structure

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.”

Reading a CSV File

Reading a CSV using Python’s inbuilt module called csv using csv.reader object. Following steps are required to read from a CSV file.
1) Import the csv library using import command
2) Open the CSV file
3) Use the csv.reader object to read the CSV file
4) Extract the field names
5) Extract the rows/records
6) Close the file

Consider the following example..

import csv

file = open('Salary_Data.csv')

csvreader = csv.reader(file)

header = []
header = next(csvreader)
header

rows = []
for row in csvreader:
        rows.append(row)
rows

file.close()

Implementing the same code using the with open command.

import csv
rows = []
with open("Salary_Data.csv", 'r) as file:
    csvreader = csv.reader(file)
    header = next(csvreader)
    for row in csvreader:
        rows.append(row)
print(header)
print(rows)

Writing in a CSV File

We can write to a CSV file in multiple ways.

a) Using csv.writerow to write one row at a time
b) Using csv.writerows() to write mutiple lines at a time. Consider the example given below

#importing the csv module
import csv
 
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
 
# data rows of csv file
rows = [ ['Ali', 'COE', '2', '9.0'],
        ['Danish', 'COE', '2', '9.1'],
        ['Baber', 'IT', '2', '9.3'],
        ['Sagar', 'SE', '1', '9.5'],
        ['Rehman', 'MCE', '3', '7.8'],
        ['Nadeem', 'EP', '2', '9.1']]
 
# name of csv file
filename = "college_records.csv"
 
# writing to csv file
with open(filename, 'w') as csvfile:

    # creating a csv writer object
    csvwriter = csv.writer(csvfile)
     
    # writing the fields
    csvwriter.writerow(fields)
     
    # writing the data rows
    csvwriter.writerows(rows)

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *