SlideShare a Scribd company logo
CSV FILE MANIPULATION
STRUCTURED TEXT FILES
• Simple text files are a collection of lines with an escape sequence at
the end of each line.
• There is no definitive way to identify specific pieces of information
unless there is a specified format to the file.
• Ex. /etc/passwd
username:*:UID:GID: name: home Path: shell
• However there are several structured files
• Tab Delimited – values separated with a tab
• CSV – values separated with a ‘,’
• HTML/XML – tags , ‘< >’
COMMA SEPARATED VALUES
• Delimited files are a common format often used as an exchange
format for spreadsheets and databases.
• Each line in a CSV file represents a row in the spreadhseet
• Usually there is a header that denoted each of the column names.
• Since CSV’s are a formatted text file they can still have end of line
escape sequencesID Term Course Grade
800412564 201652 ISY150 A
800798465 201652 CIS120 A
800125498 201652 CIS120 C
800174658 201652 CIS150 F
MANIPULATING CSV FILES VS. PLAIN TEXT
FILES
• Since CSV files are just formatted text files the process to read them
is similar to processing text files.
• Create a file stream, create reader/writer object, process the reader/writer, close
stream
• When files are read in they need to be processed as lists(arrays) and
each element is a unique element in the array that does not need to
be split.
• There is a unique module for processing csv files
• Code: import csv
READ CSV EXAMPLE
import csv
exFile = open(‘example.csv’ , ‘r’)
exReader = csv.reader(exFile)
for row in exReader:
print row
exFile.close()
import csv
exFile = open(‘example.csv’ , ‘r’)
exReader = csv.reader(exFile)
exReader = list(exReader)
for i in (0, 10, 1):
print exReader[i]
exFile.close()
WRITE CSV EXAMPLE
import csv
outFile = open(‘outputFile.csv’, ‘w’)
outWriter = csv.writer(outFile)
outWriter.writerow([‘Date’, ‘ID’, ‘GPA’])
outWriter.writerow([’01/12/2015’, ‘700514323’, ‘3.0’])
outWriter.writerow([’01/12/2015’, ‘700645798’, ‘2.64’])
outFile.close()
PROCESS CSV FILES IN A DIRECTORY
EXAMPLE
import csv, os
for currFile in os.listdir(‘~/Documents’)
if (not currFile.endswith(‘.csv’)):
continue
else:
# process csv file

More Related Content

PPTX
How to process csv files
PPTX
Reading and Writing Files
PPTX
Data file handling in python binary & csv files
PPTX
31cs
PPTX
Streams and Files
PPTX
File handling
PPTX
File handling in vb.net
PDF
C++ Files and Streams
How to process csv files
Reading and Writing Files
Data file handling in python binary & csv files
31cs
Streams and Files
File handling
File handling in vb.net
C++ Files and Streams

What's hot (20)

PPT
File handling
PPTX
32sql server
PPTX
Filehandling
PPT
ASP.NET Session 7
PPTX
Files in c++
PPTX
30csharp
PPT
14 file handling
 
PPT
File Handling In C++(OOPs))
PPT
Cpp file-handling
PDF
PPTX
C# File IO Operations
PPTX
An Introduction To Python - Files, Part 1
PPTX
2CPP17 - File IO
DOCX
File handling in c++
PDF
Filehadnling
PPT
working file handling in cpp overview
PPTX
File handling in c++
PPT
Data file handling
PPT
C++ files and streams
File handling
32sql server
Filehandling
ASP.NET Session 7
Files in c++
30csharp
14 file handling
 
File Handling In C++(OOPs))
Cpp file-handling
C# File IO Operations
An Introduction To Python - Files, Part 1
2CPP17 - File IO
File handling in c++
Filehadnling
working file handling in cpp overview
File handling in c++
Data file handling
C++ files and streams
Ad

Viewers also liked (14)

PPTX
Python Basics
PPTX
PPTX
Processing Regex Python
PPTX
Web Scraping
PPTX
Sending Email
PPTX
More Pattern Matching With RegEx
PPTX
Processing with Regular Expressions
PPTX
File I/O
PPTX
More Perl Basics
PPTX
Matching with Regular Expressions
PPTX
Passing Arguments
PPTX
Regular Expressions
PPTX
Subroutines
PPTX
Examining Linux File Structures
Python Basics
Processing Regex Python
Web Scraping
Sending Email
More Pattern Matching With RegEx
Processing with Regular Expressions
File I/O
More Perl Basics
Matching with Regular Expressions
Passing Arguments
Regular Expressions
Subroutines
Examining Linux File Structures
Ad

Similar to CSV File Manipulation (20)

PPTX
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
PPTX
HTML table with example code-different types of HTML lists - ordered lists an...
PDF
CSV Files-1.pdf
PPTX
H file handling
PPTX
Data file handling in python introduction,opening & closing files
PPTX
Data file handling in python introduction,opening &amp; closing files
PPTX
CSV_FILES.pptx
PPTX
oops (1).pptx
PPTX
what are python basics.pptx.Join Python training in Chandigarh
PDF
Csv python-project
PPTX
01 file handling for class use class pptx
PDF
Dealing with files in python specially CSV files
PDF
Python - Lecture 11
PPTX
1 cs xii_python_file_handling text n binary file
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
PDF
file handling.pdf
PPTX
Binary File.pptx
PPTX
file handling.pptx avlothaan pa thambi popa
PPTX
File Handling
PDF
Chapter-12eng-Data-Transfer-Between-Files-SQL-Databases-and-DataFrames.pdf
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
HTML table with example code-different types of HTML lists - ordered lists an...
CSV Files-1.pdf
H file handling
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening &amp; closing files
CSV_FILES.pptx
oops (1).pptx
what are python basics.pptx.Join Python training in Chandigarh
Csv python-project
01 file handling for class use class pptx
Dealing with files in python specially CSV files
Python - Lecture 11
1 cs xii_python_file_handling text n binary file
FILE HANDLING IN PYTHON Presentation Computer Science
file handling.pdf
Binary File.pptx
file handling.pptx avlothaan pa thambi popa
File Handling
Chapter-12eng-Data-Transfer-Between-Files-SQL-Databases-and-DataFrames.pdf

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Variable Scope
PPTX
Returning Data
PPTX
Intro to Functions
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Nested Loops
PPT
Conditional Loops
PPTX
Introduction to Repetition Structures
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
Software Development Life Cycle
Variable Scope
Returning Data
Intro to Functions
Introduction to GUIs with guizero
Function Parameters
Nested Loops
Conditional Loops
Introduction to Repetition Structures
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security

Recently uploaded (20)

PPTX
Surgical thesis protocol formation ppt.pptx
PPTX
STS CHAP 4 human development as reflected
PPTX
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
PDF
LSR CASEBOOK 2024-25.pdf. very nice casbook
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
A slide for students with the advantagea
PPT
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PPTX
Prokaryotes v Eukaryotes PowerPoint.pptx
PDF
Beginner’s Guide to Digital Marketing.pdf
PPTX
Theory of Change. AFH-FRDP OCEAN ToCpptx
PDF
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
PPT
APPROACH TO DEVELOPMENTALlllllllllllllllll
DOC
field study for teachers graduating samplr
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PDF
Entrepreneurship PowerPoint for students
PPTX
The Stock at arrangement the stock and product.pptx
PDF
Shopify Store Management_ Complete Guide to E-commerce Success.pdf
DOCX
mcsp232projectguidelinesjan2023 (1).docx
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
Surgical thesis protocol formation ppt.pptx
STS CHAP 4 human development as reflected
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
LSR CASEBOOK 2024-25.pdf. very nice casbook
Blue-Modern-Elegant-Presentation (1).pdf
A slide for students with the advantagea
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Prokaryotes v Eukaryotes PowerPoint.pptx
Beginner’s Guide to Digital Marketing.pdf
Theory of Change. AFH-FRDP OCEAN ToCpptx
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
APPROACH TO DEVELOPMENTALlllllllllllllllll
field study for teachers graduating samplr
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
Entrepreneurship PowerPoint for students
The Stock at arrangement the stock and product.pptx
Shopify Store Management_ Complete Guide to E-commerce Success.pdf
mcsp232projectguidelinesjan2023 (1).docx
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs

CSV File Manipulation

  • 2. STRUCTURED TEXT FILES • Simple text files are a collection of lines with an escape sequence at the end of each line. • There is no definitive way to identify specific pieces of information unless there is a specified format to the file. • Ex. /etc/passwd username:*:UID:GID: name: home Path: shell • However there are several structured files • Tab Delimited – values separated with a tab • CSV – values separated with a ‘,’ • HTML/XML – tags , ‘< >’
  • 3. COMMA SEPARATED VALUES • Delimited files are a common format often used as an exchange format for spreadsheets and databases. • Each line in a CSV file represents a row in the spreadhseet • Usually there is a header that denoted each of the column names. • Since CSV’s are a formatted text file they can still have end of line escape sequencesID Term Course Grade 800412564 201652 ISY150 A 800798465 201652 CIS120 A 800125498 201652 CIS120 C 800174658 201652 CIS150 F
  • 4. MANIPULATING CSV FILES VS. PLAIN TEXT FILES • Since CSV files are just formatted text files the process to read them is similar to processing text files. • Create a file stream, create reader/writer object, process the reader/writer, close stream • When files are read in they need to be processed as lists(arrays) and each element is a unique element in the array that does not need to be split. • There is a unique module for processing csv files • Code: import csv
  • 5. READ CSV EXAMPLE import csv exFile = open(‘example.csv’ , ‘r’) exReader = csv.reader(exFile) for row in exReader: print row exFile.close() import csv exFile = open(‘example.csv’ , ‘r’) exReader = csv.reader(exFile) exReader = list(exReader) for i in (0, 10, 1): print exReader[i] exFile.close()
  • 6. WRITE CSV EXAMPLE import csv outFile = open(‘outputFile.csv’, ‘w’) outWriter = csv.writer(outFile) outWriter.writerow([‘Date’, ‘ID’, ‘GPA’]) outWriter.writerow([’01/12/2015’, ‘700514323’, ‘3.0’]) outWriter.writerow([’01/12/2015’, ‘700645798’, ‘2.64’]) outFile.close()
  • 7. PROCESS CSV FILES IN A DIRECTORY EXAMPLE import csv, os for currFile in os.listdir(‘~/Documents’) if (not currFile.endswith(‘.csv’)): continue else: # process csv file

Editor's Notes

  • #4: CSV vs escel and otherspreadsheets No types – all strings No fonts, sizes or colors No multiple worsheets No cell widths or heights No merged cells No images or charts