SlideShare a Scribd company logo
How to process
CSV file in Python?
By
Tukaram Bhagat
Why Learn Python?
• Python is a general-purpose, versatile and
popular programming language. It's great as a
first language because it is concise and easy to
read, and it is also a good language to have in
any programmer's stack as it can be used for
everything from web development to
software development and data science
applications.
Take-Away Skills
• This course is a great introduction to both
fundamental programming concepts and the
Python programming language. Python 3 is
the most up-to-date version of the language
with many improvements made to increase
the efficiency and simplicity of the code that
you write.
What Is a CSV File?
A CSV file (Comma Separated Values file) is a
plain text file that uses specific structuring to
arrange tabular data.
It can contain only actual text
printable ASCII or Unicode characters.
Where Do CSV Files Come From?
• CSV files are normally created by programs
that handle large amounts of data.
• They are a convenient way to export data
from spreadsheets and databases as well as
import or use it in other programs.
Parsing CSV Files With Python’s Built-
in CSV Library
• The csv library provides functionality to both
read from and write to CSV files.
• The csv library contains objects and other
code to read, write, and process data from
and to CSV files.
Reading CSV Files With csv
Here’s the employee_birthday.txt file:
Name,Department,Birthday Month
John Smith,Accounting,November
Erica Meyers,IT,March
Here’s code to read it:
import csv
with open('employee_birthday.txt') as csv_file:
csv_reader = csv.reader(csv_file , delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(f't{row[0]} works in the {row[1]} department, and was born in
{row[2]}.')
line_count += 1
print(f'Processed {line_count} lines.')
Writing CSV Files With csv
import csv
with open('employee_file.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file , delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(['John Smith', 'Accounting',
'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

More Related Content

PDF
Python - Lecture 11
PDF
Csv python-project
PPTX
CSV File Manipulation
PPTX
2CPP17 - File IO
PDF
C++ Files and Streams
PPTX
Reading and Writing Files
PPTX
Stream classes in C++
Python - Lecture 11
Csv python-project
CSV File Manipulation
2CPP17 - File IO
C++ Files and Streams
Reading and Writing Files
Stream classes in C++

What's hot (19)

PPT
working file handling in cpp overview
PPTX
File handling
PDF
08. handling file streams
PPTX
32sql server
PPTX
30csharp
PPTX
Streams and Files
PPT
17 files and streams
PDF
Files and streams
PPTX
Data file handling in python binary & csv files
PPTX
Pf cs102 programming-8 [file handling] (1)
PPT
Filehandlinging cp2
PDF
Chapter28 data-file-handling
PPT
Data file handling
PPTX
CPP17 - File IO
DOCX
Filehandling
PPT
23 priority queue
PDF
Hadoop first mr job - inverted index construction
PPT
14 file handling
 
working file handling in cpp overview
File handling
08. handling file streams
32sql server
30csharp
Streams and Files
17 files and streams
Files and streams
Data file handling in python binary & csv files
Pf cs102 programming-8 [file handling] (1)
Filehandlinging cp2
Chapter28 data-file-handling
Data file handling
CPP17 - File IO
Filehandling
23 priority queue
Hadoop first mr job - inverted index construction
14 file handling
 
Ad

Similar to How to process csv files (20)

PPTX
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
PDF
Class 12th IP project on buisness management
PPTX
Understanding Character Encodings
PPTX
Introduction-to-Python-Programming1.pptx
PPTX
oops (1).pptx
PPTX
It is about IDLE Python Installation version 3.1.2
PPTX
Toolboxes for data scientists
PDF
Chapter 6
PPTX
MODULE 1.pptx
PPTX
Certified Python Programming Course in Delhi
PPTX
Certified Python Training Courses in Chennai
PPTX
Python Certification Course In Hyderabad
PPTX
Certified Python Developer Courses in Mumbai
PPTX
Python Certification courses in Ahmedabad
PPTX
Python Certification Course in Bangalore
PPTX
Certified Python Training Course in Bhubaneshwar
PPTX
Python Programming Certification Course In Pune
PDF
Chapter-6.pdf
PPTX
Top Skills You Need As a Python Developer.pptx
PDF
The essence of the VivaCore code analysis library
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
Class 12th IP project on buisness management
Understanding Character Encodings
Introduction-to-Python-Programming1.pptx
oops (1).pptx
It is about IDLE Python Installation version 3.1.2
Toolboxes for data scientists
Chapter 6
MODULE 1.pptx
Certified Python Programming Course in Delhi
Certified Python Training Courses in Chennai
Python Certification Course In Hyderabad
Certified Python Developer Courses in Mumbai
Python Certification courses in Ahmedabad
Python Certification Course in Bangalore
Certified Python Training Course in Bhubaneshwar
Python Programming Certification Course In Pune
Chapter-6.pdf
Top Skills You Need As a Python Developer.pptx
The essence of the VivaCore code analysis library
Ad

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
GDM (1) (1).pptx small presentation for students
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
GDM (1) (1).pptx small presentation for students
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
STATICS OF THE RIGID BODIES Hibbelers.pdf
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

How to process csv files

  • 1. How to process CSV file in Python? By Tukaram Bhagat
  • 2. Why Learn Python? • Python is a general-purpose, versatile and popular programming language. It's great as a first language because it is concise and easy to read, and it is also a good language to have in any programmer's stack as it can be used for everything from web development to software development and data science applications.
  • 3. Take-Away Skills • This course is a great introduction to both fundamental programming concepts and the Python programming language. Python 3 is the most up-to-date version of the language with many improvements made to increase the efficiency and simplicity of the code that you write.
  • 4. What Is a CSV File? A CSV file (Comma Separated Values file) is a plain text file that uses specific structuring to arrange tabular data. It can contain only actual text printable ASCII or Unicode characters.
  • 5. Where Do CSV Files Come From? • CSV files are normally created by programs that handle large amounts of data. • They are a convenient way to export data from spreadsheets and databases as well as import or use it in other programs.
  • 6. Parsing CSV Files With Python’s Built- in CSV Library • The csv library provides functionality to both read from and write to CSV files. • The csv library contains objects and other code to read, write, and process data from and to CSV files.
  • 7. Reading CSV Files With csv Here’s the employee_birthday.txt file: Name,Department,Birthday Month John Smith,Accounting,November Erica Meyers,IT,March
  • 8. Here’s code to read it: import csv with open('employee_birthday.txt') as csv_file: csv_reader = csv.reader(csv_file , delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: print(f'Column names are {", ".join(row)}') line_count += 1 else: print(f't{row[0]} works in the {row[1]} department, and was born in {row[2]}.') line_count += 1 print(f'Processed {line_count} lines.')
  • 9. Writing CSV Files With csv import csv with open('employee_file.csv', mode='w') as employee_file: employee_writer = csv.writer(employee_file , delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) employee_writer.writerow(['John Smith', 'Accounting', 'November']) employee_writer.writerow(['Erica Meyers', 'IT', 'March'])