How to Read Csv Uploaded File in Python

Intro: In this article, I will walk you through the different 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." Information technology is the simplest form of storing data in tabular form as patently text. Information technology is of import to know to work with CSV because we mostly rely on CSV data in our 24-hour interval-to-day lives as information scientists.

Structure of CSV:

We accept 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 ascertainment/a record. The values of a record are separated past "comma."

2. Reading a CSV

CSV files can be handled in multiple ways in Python.

2.1 Using csv.reader

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

Steps to read a CSV file:

1. 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('Salary_Data.csv')  type(file)

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

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

csvreader = csv.reader(file)

4. Extract the field names

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

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

The first fourth dimension you run next() it returns the header and the adjacent time yous run information technology returns the first record and then on.

header = [] header = side by side(csvreader) header

5. Extract the rows/records

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

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

6. Close the file

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

file.close()

Complete Code:

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

Naturally, nosotros might forget to close an open file. To avoid that we can use the with()argument to automatically release the resource. In simple terms, in that location is no need to call the .close() method if we are using with() argument.

Implementing the in a higher place lawmaking using with() statement:

Syntax: with open(filename, fashion) every bit alias_filename:

Modes:

'r' – to read an existing file,
'westward' – to create a new file if the given file doesn't be and write to information technology,
'a' – to suspend 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) print(header) print(rows)

2.two Using .readlines()

Now the question is – "Is it possible to fetch the header, rows using only open() and with() statements and without the csv library?" Let'southward run into…

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

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

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

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

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

Here comes the pandas library into the picture.

two.three Using pandas

Steps of reading CSV files using pandas

i. Import pandas library

import pandas as pd

2. Load CSV files to pandas using read_csv()

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

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

3. Extract the field names

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

data.columns

four. Extract the rows

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

data.Salary

three. Writing to a CSV file

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

3.1 Using csv.writer

Let'due south assume we are recording iii Students information(Name, M1 Score, M2 Score)

header = ['Proper name', 'M1 Score', 'M2 Score'] data = [['Alex', 62, fourscore], ['Brad', 45, 56], ['Joey', 85, 98]]

Steps of writing to a CSV file:

1. Import csv library

import csv

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

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

4. Write the header

five. Write the rest of the information

code for steps 2-5

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

Beneath is how our CSV file looks.

3.2 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'] information = [['Alex', 62, 80], ['Brad', 45, 56], ['Joey', 85, 98]] filename = 'Student_scores.csv' with open up(filename, 'westward') every bit file:     for header in header:         file.write(str(header)+', ')     file.write('n')     for row in data:         for ten in row:             file.write(str(x)+', ')         file.write('n')

3.3. Using pandas

Steps to writing to a CSV using pandas

1. Import pandas library

import pandas as pd

2. Create a pandas dataframe using pd.DataFrame

Syntax: pd.DataFrame(data, columns)

The information 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]] data = pd.DataFrame(information, columns=header)

iii. Write to a CSV file using to_csv()

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

**separator is ',' by default.

alphabetize=Faux to remove the alphabetize numbers.

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

Below is how our CSV looks like

Stop Notes:

Thank you for reading till the conclusion. By the cease of this article, we are familiar with different ways of handling CSV files in Python.

I hope this article is informative. Feel free to share it with your report buddies.

References:

Check out the complete code from the GitHub repo.

Other Web log Posts past me

Feel free to check out my other blog posts from my Analytics Vidhya Contour.

You can find me on LinkedIn, Twitter in case you lot would desire to connect. I would exist glad to connect with you.

For firsthand exchange of thoughts, delight write to me at [electronic mail protected].

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

millertiod1964.blogspot.com

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

0 Response to "How to Read Csv Uploaded File in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel