SlideShare a Scribd company logo
1 of 60
Download to read offline
XII. FILE PROCESSING
Engr. Ranel O. Padon
PYTHON PROGRAMMING TOPICS
I

• Introduction to Python Programming

II

• Python Basics

III

• Controlling the Program Flow

IV

• Program Components: Functions, Classes, Packages, and Modules

V

• Sequences (List and Tuples), and Dictionaries

VI

• Object-Based Programming: Classes and Objects

VII

• Customizing Classes and Operator Overloading

VIII

• Object-Oriented Programming: Inheritance and Polymorphism

IX

• Randomization Algorithms

X

• Exception Handling and Assertions

XI

• String Manipulation and Regular Expressions

XII

• File Handling and Processing

XIII

• GUI Programming Using Tkinter
Data Hierarchy

File
Processing

File-Open Modes
Dissecting Files
The Power of Buffering
FILE HANDLING

variables offer only temporary storage of data
they are lost when they “goes out of scope” or
when the program terminates
FILE HANDLING

files are used for long-term retention of
large amounts of data, even after the program
that created the data terminates.
data maintained in files is called persistent data
FILE HANDLING | Data Hierarchy

Bit (“Binary digit”) => the smallest computer data item
Bit is a digit that can assume one of two values
FILE HANDLING | Data Hierarchy

Programming with low-level bit formats is tedious & boring.
use decimal digits, letters, and symbols instead.
FILE HANDLING | Data Hierarchy

Characters are made-up of digits, letters, and characters.
Characters are represented as combination of bits (bytes).
FILE HANDLING | Data Hierarchy
FILE HANDLING | Data Hierarchy
Field (Column) is a collection of characters,
represented as words.
Record (Row) is a collection of fields,
represented as a tuple, dictionary, instance of a class.
File (Table) is a collection of records,
implemented as sequential access or random-access.
Database (Folder) is a collection of files,
handled by DBMS softwares.
FILE HANDLING | Data Hierarchy
FILE HANDLING | Data Hierarchy
FILE HANDLING | open() & close()
magical_file = open(“file_name.txt” [, a|r|r+|w|w+] [, buffer_mode])

magical_file.close()
FILE HANDLING | Other Functions
FILE HANDLING | open()

Open Mode

r
r+
w
w+
a
a+

Read

Write

Appends

Overwrites

Creates

Cursor @
Start

Cursor @
EOF
FILE HANDLING | Common Modes

Open Mode

r
w

Read

Write

Appends

Overwrites

Creates

Cursor @
Start

Cursor @
EOF
FILE HANDLING | open()
“r” is the default file-open mode
open(“input.dat”) = open(“input.dat”, “r”)
FILE HANDLING | r
FILE HANDLING | r
FILE HANDLING | r
FILE HANDLING | w

 try removing line #6
 try removing "n" in lines #3 and #4
FILE HANDLING | w
FILE HANDLING | with-as Keyword
FILE HANDLING | Parsing

Paninda.txt
FILE HANDLING | Parsing | split
FILE HANDLING | Parsing | split
FILE HANDLING | Parsing | csv

Paranormal_Sightings.csv
FILE HANDLING | Parsing | strip
FILE HANDLING | Parsing | strip
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | HTML Parsing
MangJose.html
FILE HANDLING | HTML Parsing
MangJose.html
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | r+, w+, a+
All of the "plus" modes allow reading and writing:
the main difference between them is where
we're positioned in the file.

“r+” puts us at the beginning
“w+” puts us at the beginning & the end,
because the file's truncated
“a+” puts us at the end.
FILE HANDLING | w+
FILE HANDLING | Buffering
FILE HANDLING | Buffering
“-1” is the default file-open buffering mode
open(“input.dat”) = open(“input.dat”, “r”, “-1”)
Flag

Meaning

0

unbuffered

1

buffered line

n

buffered with size n

-1

system default
FILE HANDLING | Creating A Big File!
FILE HANDLING | Unbuffered r
Then, let’s read that big file. 
FILE HANDLING | Buffered r
Now, with the help of buffering. 
FILE HANDLING | Buffered By Default

In other languages, like C or Java,
buffering is not the default mode.
FILE HANDLING | What else?
1. Random-Access Files: for fast searching/editing of records
* use the shelve module
* shelve.open()
2. Serialization: compressing file as objects for efficiency;
useful for transferring data (objects, sequences, etc)
across a network connection or saving states of a game
* use the pickle or cPickle module
* cPickle.dump(stringList_to_be_written, serialized_file)
* records = cPickle.load(serialized_file)
PRACTICE EXERCISE| MORSE CODE
PRACTICE EXERCISE| MC CHART
PRACTICE EXERCISE| MC CHART
PRACTICE EXERCISE| MORSE CODE

A. Read a file containing Filipino/English-language
phrases and encodes it into Morse code.
B. Read a Morse code file and converts it into the
Filipino/English-language equivalent.

Use one blank between each Morse-coded letter and
three blanks between each Morse-coded word.
REFERENCES
 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/information used here have no proper source
citation, and I do not claim ownership of these either. I don’t want to reinvent the
wheel, and I just want to reuse and reintegrate materials that I think are useful or
cool, then present them in another light, form, or perspective. Moreover, the
images/information here are mainly used for illustration/educational purposes only,
in the spirit of openness of data, spreading light, and empowering people with
knowledge. 

More Related Content

What's hot

Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha BaliAkanksha Bali
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experiencedzynofustechnology
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Richard Thomson
 

What's hot (20)

Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Java8
Java8Java8
Java8
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Python
PythonPython
Python
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
What is Python?
What is Python?What is Python?
What is Python?
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Python and You Series
Python and You SeriesPython and You Series
Python and You Series
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Data file handling
Data file handlingData file handling
Data file handling
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Pointers
PointersPointers
Pointers
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
 

Viewers also liked

Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On RandomnessRanel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsRanel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsRanel Padon
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismRanel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowRanel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingRanel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The BasicsRanel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesRanel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingRanel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with DrupalRanel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. IntroductionRanel Padon
 

Viewers also liked (12)

Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
 

Similar to File Processing and Parsing in Python

03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptxqover
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve ProgramsRohan Gajre
 
File management
File managementFile management
File managementsumathiv9
 
File handing in C
File handing in CFile handing in C
File handing in Cshrishcg
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary fileSanjayKumarMahto1
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.ssuser00ad4e
 

Similar to File Processing and Parsing in Python (20)

Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Python-files
Python-filesPython-files
Python-files
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
 
File management
File managementFile management
File management
 
File handing in C
File handing in CFile handing in C
File handing in C
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Python Training
Python TrainingPython Training
Python Training
 
file handling.pdf
file handling.pdffile handling.pdf
file handling.pdf
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
finally.c.ppt
finally.c.pptfinally.c.ppt
finally.c.ppt
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
 

More from Ranel Padon

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with DrupalRanel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleRanel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationRanel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryRanel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with DrupalRanel Padon
 

More from Ranel Padon (8)

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
 

Recently uploaded

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

File Processing and Parsing in Python

  • 1. XII. FILE PROCESSING Engr. Ranel O. Padon
  • 2. PYTHON PROGRAMMING TOPICS I • Introduction to Python Programming II • Python Basics III • Controlling the Program Flow IV • Program Components: Functions, Classes, Packages, and Modules V • Sequences (List and Tuples), and Dictionaries VI • Object-Based Programming: Classes and Objects VII • Customizing Classes and Operator Overloading VIII • Object-Oriented Programming: Inheritance and Polymorphism IX • Randomization Algorithms X • Exception Handling and Assertions XI • String Manipulation and Regular Expressions XII • File Handling and Processing XIII • GUI Programming Using Tkinter
  • 4.
  • 5. FILE HANDLING variables offer only temporary storage of data they are lost when they “goes out of scope” or when the program terminates
  • 6. FILE HANDLING files are used for long-term retention of large amounts of data, even after the program that created the data terminates. data maintained in files is called persistent data
  • 7. FILE HANDLING | Data Hierarchy Bit (“Binary digit”) => the smallest computer data item Bit is a digit that can assume one of two values
  • 8. FILE HANDLING | Data Hierarchy Programming with low-level bit formats is tedious & boring. use decimal digits, letters, and symbols instead.
  • 9. FILE HANDLING | Data Hierarchy Characters are made-up of digits, letters, and characters. Characters are represented as combination of bits (bytes).
  • 10. FILE HANDLING | Data Hierarchy
  • 11. FILE HANDLING | Data Hierarchy Field (Column) is a collection of characters, represented as words. Record (Row) is a collection of fields, represented as a tuple, dictionary, instance of a class. File (Table) is a collection of records, implemented as sequential access or random-access. Database (Folder) is a collection of files, handled by DBMS softwares.
  • 12. FILE HANDLING | Data Hierarchy
  • 13. FILE HANDLING | Data Hierarchy
  • 14. FILE HANDLING | open() & close() magical_file = open(“file_name.txt” [, a|r|r+|w|w+] [, buffer_mode]) magical_file.close()
  • 15. FILE HANDLING | Other Functions
  • 16. FILE HANDLING | open() Open Mode r r+ w w+ a a+ Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  • 17. FILE HANDLING | Common Modes Open Mode r w Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  • 18. FILE HANDLING | open() “r” is the default file-open mode open(“input.dat”) = open(“input.dat”, “r”)
  • 22. FILE HANDLING | w  try removing line #6  try removing "n" in lines #3 and #4
  • 24. FILE HANDLING | with-as Keyword
  • 25. FILE HANDLING | Parsing Paninda.txt
  • 26. FILE HANDLING | Parsing | split
  • 27. FILE HANDLING | Parsing | split
  • 28. FILE HANDLING | Parsing | csv Paranormal_Sightings.csv
  • 29. FILE HANDLING | Parsing | strip
  • 30. FILE HANDLING | Parsing | strip
  • 31. FILE HANDLING | Parsing & Classes
  • 32. FILE HANDLING | Parsing & Classes
  • 33. FILE HANDLING | Parsing & Classes
  • 34. FILE HANDLING | Parsing & Classes 2
  • 35. FILE HANDLING | Parsing & Classes 2
  • 36. FILE HANDLING | Parsing & Classes 2
  • 37. FILE HANDLING | Parsing & Classes 2
  • 38. FILE HANDLING | HTML Parsing MangJose.html
  • 39. FILE HANDLING | HTML Parsing MangJose.html
  • 40. FILE HANDLING | HTML Parsing
  • 41. FILE HANDLING | HTML Parsing
  • 42. FILE HANDLING | HTML Parsing
  • 43. FILE HANDLING | HTML Parsing
  • 44. FILE HANDLING | HTML Parsing
  • 45. FILE HANDLING | HTML Parsing
  • 46. FILE HANDLING | r+, w+, a+ All of the "plus" modes allow reading and writing: the main difference between them is where we're positioned in the file. “r+” puts us at the beginning “w+” puts us at the beginning & the end, because the file's truncated “a+” puts us at the end.
  • 48. FILE HANDLING | Buffering
  • 49. FILE HANDLING | Buffering “-1” is the default file-open buffering mode open(“input.dat”) = open(“input.dat”, “r”, “-1”) Flag Meaning 0 unbuffered 1 buffered line n buffered with size n -1 system default
  • 50. FILE HANDLING | Creating A Big File!
  • 51. FILE HANDLING | Unbuffered r Then, let’s read that big file. 
  • 52. FILE HANDLING | Buffered r Now, with the help of buffering. 
  • 53. FILE HANDLING | Buffered By Default In other languages, like C or Java, buffering is not the default mode.
  • 54. FILE HANDLING | What else? 1. Random-Access Files: for fast searching/editing of records * use the shelve module * shelve.open() 2. Serialization: compressing file as objects for efficiency; useful for transferring data (objects, sequences, etc) across a network connection or saving states of a game * use the pickle or cPickle module * cPickle.dump(stringList_to_be_written, serialized_file) * records = cPickle.load(serialized_file)
  • 55.
  • 59. PRACTICE EXERCISE| MORSE CODE A. Read a file containing Filipino/English-language phrases and encodes it into Morse code. B. Read a Morse code file and converts it into the Filipino/English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.
  • 60. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 