Read a Csv File as Dict Python

Intro: In this article, I will walk you through the unlike ways of reading and writing CSV files in Python.

Table of Contents:

  1. What is a CSV?
  2. Reading a CSV
  3. Writing to a CSV

1. What is a CSV?

CSV stands for "Comma Separated Values." It is the simplest form of storing data in tabular class as plain text. Information technology is important to know to piece of work with CSV considering we mostly rely on CSV data in our day-to-day lives every bit data scientists.

Construction of CSV:

We take a file named "Salary_Data.csv." The beginning line of a CSV file is the header and contains the names of the fields/features.

Later on the header, each line of the file is an observation/a record. The values of a record are separated by "comma."

2. Reading a CSV

CSV files can be handled in multiple ways in Python.

ii.1 Using csv.reader

Reading a CSV using Python'southward inbuilt module called csv using csv.reader object.

Steps to read a CSV file:

ane. Import the csv library

import csv

two. Open the CSV file

The .open() method in python is used to open files and return a file object.

file = open up('Salary_Data.csv')  type(file)

The type of file is "_io.TextIOWrapper" which is a file object that is returned by the open up() method.

3. Utilise the csv.reader object to read the CSV file

csvreader = csv.reader(file)

4. Extract the field names

Create an empty list called header. Use the next() method to obtain the header.

The .adjacent() method returns the current row and moves to the next row.

The first time y'all run next() information technology returns the header and the next time you run it returns the start tape and so on.

header = [] header = next(csvreader) header

5. Excerpt the rows/records

Create an empty list called rows and iterate through the csvreader object and suspend each row to the rows list.

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

6. Close the file

.close() method is used to shut the opened file. Once it is closed, we cannot perform any operations on information technology.

file.close()

Complete Code:

import csv file = open("Salary_Data.csv") csvreader = csv.reader(file) header = next(csvreader) print(header) rows = [] for row in csvreader:     rows.append(row) print(rows) file.close()

Naturally, we might forget to close an open up file. To avert that we can utilize the with()argument to automatically release the resource. In simple terms, there is no need to telephone call the .close() method if we are using with() statement.

Implementing the to a higher place code using with() argument:

Syntax: with open(filename, mode) as alias_filename:

Modes:

'r' – to read an existing file,
'w' – to create a new file if the given file doesn't be and write to it,
'a' – to append to existing file content,
'+' –  to create a new file for reading and writing

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) impress(header) print(rows)

two.2 Using .readlines()

At present the question is – "Is it possible to fetch the header, rows using only open up() and with() statements and without the csv library?" Let's run across…

.readlines() method is the answer. Information technology returns all the lines in a file as a list. Each item of the list is a row of our CSV file.

The starting time row of the file.readlines() is the header and the rest of them are the records.

with open up('Salary_Data.csv') as file:     content = file.readlines() header = content[:one] rows = content[1:] print(header) print(rows)

**The 'n' from the output can exist removed using .strip() method.

What if we accept a huge dataset with hundreds of features and thousands of records. Would it exist possible to handle lists??

Here comes the pandas library into the picture.

2.3 Using pandas

Steps of reading CSV files using pandas

one. Import pandas library

import pandas every bit pd

two. Load CSV files to pandas using read_csv()

Bones Syntax: pandas.read_csv(filename, delimiter=',')

data= pd.read_csv("Salary_Data.csv") data

three. Excerpt the field names

.columns is used to obtain the header/field names.

information.columns

four. Excerpt the rows

All the data of a data frame tin be accessed using the field names.

data.Salary

3. Writing to a CSV file

We tin can write to a CSV file in multiple ways.

3.one Using csv.writer

Permit's presume we are recording three Students data(Proper name, M1 Score, M2 Score)

header = ['Name', 'M1 Score', 'M2 Score'] data = [['Alex', 62, 80], ['Brad', 45, 56], ['Joey', 85, 98]]

Steps of writing to a CSV file:

one. Import csv library

import csv

2. Define a filename and Open up the file using open()

3. Create a csvwriter object using csv.writer()

4. Write the header

5. Write the rest of the information

code for steps 2-five

filename = 'Students_Data.csv' with open(filename, 'w', newline="") as file:     csvwriter = csv.writer(file) # 2. create a csvwriter object     csvwriter.writerow(header) # 4. write the header     csvwriter.writerows(data) # 5. write the rest of the data

Beneath is how our CSV file looks.

3.two Using .writelines()

Iterate through each list and convert the list elements to a string and write to the csv file.

header = ['Name', 'M1 Score', 'M2 Score'] data = [['Alex', 62, 80], ['Brad', 45, 56], ['Joey', 85, 98]] filename = 'Student_scores.csv' with open up(filename, 'west') every bit file:     for header in header:         file.write(str(header)+', ')     file.write('north')     for row in data:         for x in row:             file.write(str(10)+', ')         file.write('n')

3.3. Using pandas

Steps to writing to a CSV using pandas

1. Import pandas library

import pandas as pd

two. Create a pandas dataframe using pd.DataFrame

Syntax: pd.DataFrame(data, columns)

The data parameter takes the records/observations and the columns parameter takes the columns/field names.

header = ['Name', 'M1 Score', 'M2 Score'] data = [['Alex', 62, 80], ['Brad', 45, 56], ['Joey', 85, 98]] information = pd.DataFrame(data, columns=header)

three. Write to a CSV file using to_csv()

Syntax: DataFrame.to_csv(filename, sep=',', alphabetize=Fake)

**separator is ',' by default.

index=False to remove the index numbers.

data.to_csv('Stu_data.csv', index=False)

Below is how our CSV looks like

End Notes:

Cheers for reading till the conclusion. By the terminate of this article, we are familiar with unlike means of treatment CSV files in Python.

I hope this article is informative. Feel gratuitous to share information technology with your report buddies.

References:

Check out the consummate lawmaking from the GitHub repo.

Other Blog Posts past me

Feel free to cheque out my other web log posts from my Analytics Vidhya Profile.

Y'all can discover me on LinkedIn, Twitter in instance you would desire to connect. I would exist glad to connect with yous.

For firsthand commutation of thoughts, please write to me at [email protected].

The media shown in this article are not owned past Analytics Vidhya and are used at the Author's discretion.

waltonwend1970.blogspot.com

Source: https://www.analyticsvidhya.com/blog/2021/08/python-tutorial-working-with-csv-file-for-data-science/

0 Response to "Read a Csv File as Dict Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel