Read Multiple Rows Csv Files in Python

CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma every bit a delimiter to split the text present in it. Or in other words, CSV files are used to shop data in tabular form. As per the proper name suggested, this file contains the information which is separated past a comma and every line of a file is considered a tape. We can create the CSV file either from notepad or using excel.

Example:

Geeks,for,geeks i,of,the, top,learning,platform

We tin create a CSV file using the following ways:

1. Using Notepad: Nosotros can create a CSV file using Notepad. In the Notepad, open a new file in which split up the values by comma and save the file with .csv extension.

2. Using Excel: Nosotros can too create a CSV file using Excel. In Excel, open up a new file in which specify each value in a different prison cell and save it with filetype CSV.

To read data row-wise from a CSV file in Python, we can apply reader and DictReader which are nowadays in the CSV module allows us to fetch data row-wise.

Using reader

Using reader we can iterate between rows of a CSV file as a list of values. It iterates over all rows in a CSV file and fetches data in each row as a listing. reader() method is present in CSV library. And then to use this reader method, first nosotros need to import the CSV library. reader object accepts a single parameter chosen fileObject (a variable that holds the CSV file).

Syntax:

csv.reader(fileobject)

Steps to read CSV file:

Step 1: In club to read rows in Python, Kickoff, we demand to load the CSV file in one object. So to load the csv file into an object apply open() method.

with open up('filename') as fileObject

While loading the file by specifying path along with filename, if you lot got whatsoever unicode error and so append r earlier path of filename

with open(r'path/filename') as fileObject          

Step ii: Create a reader object past passing the above-created file object to the reader role.

reader_obj = csv.reader(file_obj)

Step 3: Use for loop on reader object to get each row.

Instance:

Consider a CSV file named "samplecsv.csv". This file contains the following information:

Id,Name,Rating 1,Akhil,4 two,Babu,3 3,Nikhil,5

Python3

import csv

with open ( 'samplecsv.csv' ) as file_obj:

reader_obj = csv.reader(file_obj)

for row in reader_obj:

print (row)

Output:

['Id', 'Proper noun', 'Rating'] ['one', 'Akhil', 'iv'] ['ii', 'Babu', '3'] ['3', 'Nikhil', 'five']

Reading CSV file without header

Everything is fine with the to a higher place example just if we don't want cavalcade names to fetch or we can say we don't want to read the header of the file, then we use the next() method on the file object earlier creating the reader object and then that it skips the headings.

Python3

import csv

with open ( 'samplecsv.csv' ) as file_obj:

heading = next (file_obj)

reader_obj = csv.reader(file_obj)

for row in reader_obj:

impress (row)

Output:

['1', 'Akhil', '4'] ['2', 'Babu', '3'] ['three', 'Nikhil', '5']

Explanation: The above code is virtually the aforementioned as the code in the in a higher place example but a slight change is we employ the next part here that helps u.s. to skip the column names while accessing information from a CSV file i.due east. first row. If column names are required then we access them from the heading object i.e. return the result of the adjacent method.

Using DictReader

When using a reader() method nosotros can iterate over a CSV file as a listing but using the DictReader class object we can iterate over a CSV file row past row as a lexicon. This DictReader method is nowadays in the csv library. Then to apply it showtime nosotros need to import the csv library. DictReader() accepts a unmarried parameter chosen fileObject (a variable that holds the csv file).

Syntax

csv.DictReader(fileobject)

Steps to read CSV file:

Step 1: Load the CSV file using the open method in a file object.

with open up('filename') as fileObject

Step 2: Create a reader object with the help of DictReader method using fileobject.

reader_obj = csv.DictReader(file_obj)

This reader object is also known as an iterator can be used to fetch row-wise information.

Step three: Use for loop on reader object to get each row.

Instance:

Python3

import csv

with open ( 'samplecsv.csv' ) as file_obj:

reader_obj = csv.DictReader(file_obj)

for row in reader_obj:

print (row)

Output:

OrderedDict([('Id', '1'), ('Proper noun', 'Akhil'), ('Rating', '4')]) OrderedDict([('Id', '2'), ('Name', 'Babu'), ('Rating', '3')]) OrderedDict([('Id', '3'), ('Name', 'Nikhil'), ('Rating', 'v')])

Explanation: In the code, offset we loaded the CSV file named samplecsv.csv and then created a reader_object that tin be iterated to fetch each row. The returned result is in the form of Key-Value pair indicates it every bit a lexicon. And then using DictReader we read data row past row as a Dictionary.


chenhuriturnar73.blogspot.com

Source: https://www.geeksforgeeks.org/reading-rows-from-a-csv-file-in-python/

0 Response to "Read Multiple Rows Csv Files in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel