Read a Specific Value From a Column and Row in Csv in Python

CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma as a delimiter to carve up the text nowadays in it. Or in other words, CSV files are used to store data in tabular grade. Every bit per the name suggested, this file contains the data which is separated by a comma and every line of a file is considered a record. We can create the CSV file either from notepad or using excel.

Case:

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

Nosotros can create a CSV file using the following ways:

1. Using Notepad: We can create a CSV file using Notepad. In the Notepad, open a new file in which separate the values past comma and relieve the file with .csv extension.

ii. Using Excel: We tin also create a CSV file using Excel. In Excel, open up a new file in which specify each value in a different cell and save it with filetype CSV.

To read data row-wise from a CSV file in Python, we tin can utilise reader and DictReader which are present 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 listing of values. It iterates over all rows in a CSV file and fetches data in each row as a listing. reader() method is nowadays in CSV library. So to use this reader method, commencement we need to import the CSV library. reader object accepts a single parameter called fileObject (a variable that holds the CSV file).

Syntax:

csv.reader(fileobject)

Steps to read CSV file:

Footstep i: In order to read rows in Python, First, nosotros demand to load the CSV file in ane object. So to load the csv file into an object use open() method.

with open('filename') equally fileObject

While loading the file by specifying path forth with filename, if you lot got any unicode error then append r before path of filename

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

Footstep ii: Create a reader object by passing the above-created file object to the reader function.

reader_obj = csv.reader(file_obj)

Stride 3: Use for loop on reader object to go each row.

Example:

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

Id,Name,Rating ane,Akhil,4 2,Babu,three 3,Nikhil,5

Python3

import csv

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

reader_obj = csv.reader(file_obj)

for row in reader_obj:

print (row)

Output:

['Id', 'Proper name', 'Rating'] ['1', 'Akhil', '4'] ['2', 'Babu', '3'] ['iii', 'Nikhil', '5']

Reading CSV file without header

Everything is fine with the above example but if nosotros don't want column names to fetch or we can say we don't want to read the header of the file, then we employ the next() method on the file object earlier creating the reader object and then that information technology skips the headings.

Python3

import csv

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

heading = next (file_obj)

reader_obj = csv.reader(file_obj)

for row in reader_obj:

print (row)

Output:

['one', 'Akhil', 'iv'] ['2', 'Babu', '3'] ['3', 'Nikhil', '5']

Explanation: The higher up lawmaking is almost the same as the code in the higher up example just a slight alter is nosotros use the next function hither that helps usa to skip the column names while accessing data from a CSV file i.e. first row. If cavalcade names are required then nosotros admission them from the heading object i.e. return the outcome of the adjacent method.

Using DictReader

When using a reader() method we can iterate over a CSV file as a list but using the DictReader class object we can iterate over a CSV file row by row as a dictionary. This DictReader method is present in the csv library. Then to utilize information technology starting time we need to import the csv library. DictReader() accepts a unmarried parameter called 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 up method in a file object.

with open('filename') as fileObject

Footstep 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 exist used to fetch row-wise data.

Footstep 3: Utilise for loop on reader object to get each row.

Example:

Python3

import csv

with open up ( 'samplecsv.csv' ) equally 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', 'ii'), ('Name', 'Babu'), ('Rating', '3')]) OrderedDict([('Id', 'iii'), ('Proper noun', 'Nikhil'), ('Rating', '5')])

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


rodrigueswastures1945.blogspot.com

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

0 Response to "Read a Specific Value From a Column and Row in Csv in Python"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel