Python Programming - XII. File Processing

1,831 views
1,658 views

Published on

Published in: Technology
3 Comments
6 Likes
Statistics
Notes
  • Getting Started with Processing: A Hands-On Introduction to Making Interactive Graphics --- http://amzn.to/1pC7lY0
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Learning Processing, Second Edition: A Beginner's Guide to Programming Images, Animation, and Interaction (The Morgan Kaufmann Series in Computer Graphics) --- http://amzn.to/1RsFJki
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Processing: A Programming Handbook for Visual Designers and Artists (MIT Press) --- http://amzn.to/1UdwVPX
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,831
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
213
Comments
3
Likes
6
Embeds 0
No embeds

No notes for slide

Python Programming - XII. File Processing

  1. 1. XII. FILE PROCESSING Engr. Ranel O. Padon
  2. 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
  3. 3. Data Hierarchy File Processing File-Open Modes Dissecting Files The Power of Buffering
  4. 4. FILE HANDLING variables offer only temporary storage of data they are lost when they “goes out of scope” or when the program terminates
  5. 5. 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
  6. 6. FILE HANDLING | Data Hierarchy Bit (“Binary digit”) => the smallest computer data item Bit is a digit that can assume one of two values
  7. 7. FILE HANDLING | Data Hierarchy Programming with low-level bit formats is tedious & boring. use decimal digits, letters, and symbols instead.
  8. 8. FILE HANDLING | Data Hierarchy Characters are made-up of digits, letters, and characters. Characters are represented as combination of bits (bytes).
  9. 9. FILE HANDLING | Data Hierarchy
  10. 10. 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.
  11. 11. FILE HANDLING | Data Hierarchy
  12. 12. FILE HANDLING | Data Hierarchy
  13. 13. FILE HANDLING | open() & close() magical_file = open(“file_name.txt” [, a|r|r+|w|w+] [, buffer_mode]) magical_file.close()
  14. 14. FILE HANDLING | Other Functions
  15. 15. FILE HANDLING | open() Open Mode r r+ w w+ a a+ Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  16. 16. FILE HANDLING | Common Modes Open Mode r w Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  17. 17. FILE HANDLING | open() “r” is the default file-open mode open(“input.dat”) = open(“input.dat”, “r”)
  18. 18. FILE HANDLING | r
  19. 19. FILE HANDLING | r
  20. 20. FILE HANDLING | r
  21. 21. FILE HANDLING | w  try removing line #6  try removing "n" in lines #3 and #4
  22. 22. FILE HANDLING | w
  23. 23. FILE HANDLING | with-as Keyword
  24. 24. FILE HANDLING | Parsing Paninda.txt
  25. 25. FILE HANDLING | Parsing | split
  26. 26. FILE HANDLING | Parsing | split
  27. 27. FILE HANDLING | Parsing | csv Paranormal_Sightings.csv
  28. 28. FILE HANDLING | Parsing | strip
  29. 29. FILE HANDLING | Parsing | strip
  30. 30. FILE HANDLING | Parsing & Classes
  31. 31. FILE HANDLING | Parsing & Classes
  32. 32. FILE HANDLING | Parsing & Classes
  33. 33. FILE HANDLING | Parsing & Classes 2
  34. 34. FILE HANDLING | Parsing & Classes 2
  35. 35. FILE HANDLING | Parsing & Classes 2
  36. 36. FILE HANDLING | Parsing & Classes 2
  37. 37. FILE HANDLING | HTML Parsing MangJose.html
  38. 38. FILE HANDLING | HTML Parsing MangJose.html
  39. 39. FILE HANDLING | HTML Parsing
  40. 40. FILE HANDLING | HTML Parsing
  41. 41. FILE HANDLING | HTML Parsing
  42. 42. FILE HANDLING | HTML Parsing
  43. 43. FILE HANDLING | HTML Parsing
  44. 44. FILE HANDLING | HTML Parsing
  45. 45. 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.
  46. 46. FILE HANDLING | w+
  47. 47. FILE HANDLING | Buffering
  48. 48. 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
  49. 49. FILE HANDLING | Creating A Big File!
  50. 50. FILE HANDLING | Unbuffered r Then, let’s read that big file. 
  51. 51. FILE HANDLING | Buffered r Now, with the help of buffering. 
  52. 52. FILE HANDLING | Buffered By Default In other languages, like C or Java, buffering is not the default mode.
  53. 53. 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)
  54. 54. PRACTICE EXERCISE| MORSE CODE
  55. 55. PRACTICE EXERCISE| MC CHART
  56. 56. PRACTICE EXERCISE| MC CHART
  57. 57. 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.
  58. 58. 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. 

×