SlideShare a Scribd company logo
1 of 7
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

What's hot (20)

File handling
File handlingFile handling
File handling
 
32sql server
32sql server32sql server
32sql server
 
Filehandling
FilehandlingFilehandling
Filehandling
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
 
Files in c++
Files in c++Files in c++
Files in c++
 
30csharp
30csharp30csharp
30csharp
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
14 file handling
14 file handling14 file handling
14 file handling
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
Files in java
Files in javaFiles in java
Files in java
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Data file handling
Data file handlingData file handling
Data file handling
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 

Viewers also liked

Viewers also liked (14)

Python Basics
Python BasicsPython Basics
Python Basics
 
Json
JsonJson
Json
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Python
 
Web Scraping
Web ScrapingWeb Scraping
Web Scraping
 
Sending Email
Sending EmailSending Email
Sending Email
 
More Pattern Matching With RegEx
More Pattern Matching With RegExMore Pattern Matching With RegEx
More Pattern Matching With RegEx
 
Processing with Regular Expressions
Processing with Regular ExpressionsProcessing with Regular Expressions
Processing with Regular Expressions
 
File I/O
File I/OFile I/O
File I/O
 
More Perl Basics
More Perl BasicsMore Perl Basics
More Perl Basics
 
Matching with Regular Expressions
Matching with Regular ExpressionsMatching with Regular Expressions
Matching with Regular Expressions
 
Passing Arguments
Passing ArgumentsPassing Arguments
Passing Arguments
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Examining Linux File Structures
Examining Linux File StructuresExamining Linux File Structures
Examining Linux File Structures
 

Similar to Manipulate and process CSV files

Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV ConnectivityNeeru Mittal
 
Data file handling in python binary & csv files
Data file handling in python binary & csv filesData file handling in python binary & csv files
Data file handling in python binary & csv filesKeerty Smile
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsMicrosoft Tech Community
 
U-SQL Reading & Writing Files (SQLBits 2016)
U-SQL Reading & Writing Files (SQLBits 2016)U-SQL Reading & Writing Files (SQLBits 2016)
U-SQL Reading & Writing Files (SQLBits 2016)Michael Rys
 
2 introduction css
2 introduction css2 introduction css
2 introduction cssJalpesh Vasa
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to HiveDatabricks
 
Css Founder.com | Cssfounder in
Css Founder.com | Cssfounder inCss Founder.com | Cssfounder in
Css Founder.com | Cssfounder inCss Founder
 
MTA css flow, positioning, and styling
MTA css flow, positioning, and stylingMTA css flow, positioning, and styling
MTA css flow, positioning, and stylingDhairya Joshi
 
Publishing xml
Publishing xmlPublishing xml
Publishing xmlKumar
 

Similar to Manipulate and process CSV files (20)

Python - Lecture 11
Python - Lecture 11Python - Lecture 11
Python - Lecture 11
 
oops (1).pptx
oops (1).pptxoops (1).pptx
oops (1).pptx
 
Bigdata
BigdataBigdata
Bigdata
 
Moving Data to and From R
Moving Data to and From RMoving Data to and From R
Moving Data to and From R
 
CSV Files-1.pdf
CSV Files-1.pdfCSV Files-1.pdf
CSV Files-1.pdf
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
 
CSV import in AtoM
CSV import in AtoMCSV import in AtoM
CSV import in AtoM
 
Data file handling in python binary & csv files
Data file handling in python binary & csv filesData file handling in python binary & csv files
Data file handling in python binary & csv files
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analytics
 
U-SQL Reading & Writing Files (SQLBits 2016)
U-SQL Reading & Writing Files (SQLBits 2016)U-SQL Reading & Writing Files (SQLBits 2016)
U-SQL Reading & Writing Files (SQLBits 2016)
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
 
Splitting up text
Splitting up textSplitting up text
Splitting up text
 
Css present
Css presentCss present
Css present
 
CSV_FILES.pptx
CSV_FILES.pptxCSV_FILES.pptx
CSV_FILES.pptx
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to Hive
 
Css Founder.com | Cssfounder in
Css Founder.com | Cssfounder inCss Founder.com | Cssfounder in
Css Founder.com | Cssfounder in
 
Dataweave
DataweaveDataweave
Dataweave
 
MTA css flow, positioning, and styling
MTA css flow, positioning, and stylingMTA css flow, positioning, and styling
MTA css flow, positioning, and styling
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
Html and css
Html and cssHtml and css
Html and css
 

More from primeteacher32

More from primeteacher32 (20)

Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
Variable Scope
Variable ScopeVariable Scope
Variable Scope
 
Returning Data
Returning DataReturning Data
Returning Data
 
Intro to Functions
Intro to FunctionsIntro to Functions
Intro to Functions
 
Introduction to GUIs with guizero
Introduction to GUIs with guizeroIntroduction to GUIs with guizero
Introduction to GUIs with guizero
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Nested Loops
Nested LoopsNested Loops
Nested Loops
 
Conditional Loops
Conditional LoopsConditional Loops
Conditional Loops
 
Introduction to Repetition Structures
Introduction to Repetition StructuresIntroduction to Repetition Structures
Introduction to Repetition Structures
 
Input Validation
Input ValidationInput Validation
Input Validation
 
Windows File Systems
Windows File SystemsWindows File Systems
Windows File Systems
 
Nesting Conditionals
Nesting ConditionalsNesting Conditionals
Nesting Conditionals
 
Conditionals
ConditionalsConditionals
Conditionals
 
Intro to Python with GPIO
Intro to Python with GPIOIntro to Python with GPIO
Intro to Python with GPIO
 
Variables and Statements
Variables and StatementsVariables and Statements
Variables and Statements
 
Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Raspberry Pi
Raspberry PiRaspberry Pi
Raspberry Pi
 
Hardware vs. Software Presentations
Hardware vs. Software PresentationsHardware vs. Software Presentations
Hardware vs. Software Presentations
 
Block chain security
Block chain securityBlock chain security
Block chain security
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceanilsa9823
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girlsshivangimorya083
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceSanjay Bokadia
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls DubaiDark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls Dubaikojalkojal131
 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackSuhani Kapoor
 
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfNPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfDivyeshPatel234692
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testspriyanshukumar97908
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...Suhani Kapoor
 
The Impact of Socioeconomic Status on Education.pdf
The Impact of Socioeconomic Status on Education.pdfThe Impact of Socioeconomic Status on Education.pdf
The Impact of Socioeconomic Status on Education.pdftheknowledgereview1
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证obuhobo
 
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...Suhani Kapoor
 
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...Suhani Kapoor
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...gurkirankumar98700
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012sapnasaifi408
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxGry Tina Tinde
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector Experience
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls DubaiDark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
 
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCeCall Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
 
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfNPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
 
Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and tests
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
 
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
 
The Impact of Socioeconomic Status on Education.pdf
The Impact of Socioeconomic Status on Education.pdfThe Impact of Socioeconomic Status on Education.pdf
The Impact of Socioeconomic Status on Education.pdf
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
 
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
 
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...
VIP Call Girls in Jamshedpur Aarohi 8250192130 Independent Escort Service Jam...
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
 
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptx
 

Manipulate and process CSV files

  • 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

  1. 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