File I/O With Python File Opening file_reading_variable = - - PowerPoint PPT Presentation

file i o with python file opening
SMART_READER_LITE
LIVE PREVIEW

File I/O With Python File Opening file_reading_variable = - - PowerPoint PPT Presentation

File I/O With Python File Opening file_reading_variable = open(filename.txt, mode ) Three Modes to consider: r, a, w Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit amet, consectetur amet,


slide-1
SLIDE 1

File I/O With Python

slide-2
SLIDE 2

File Opening

 file_reading_variable = open(‘filename.txt’, mode)

 Three Modes to consider: ‘r’, ‘a’, ‘w’

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sollicitudin bibendum odio, non aliquet est viverra vel. Sed auctor tempor massa, at molestie felis eleifend ac. [you cannot edit the file,

  • nly read from it]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sollicitudin bibendum odio, non aliquet est viverra vel. Sed auctor tempor massa, at molestie felis eleifend

  • ac. [Starting writing to

file here] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sollicitudin bibendum odio, non aliquet est viverra vel. Sed auctor tempor massa, at molestie felis eleifend ac. [ignore existing contents, write file from the beginning]

slide-3
SLIDE 3

Read mode

 Read the file in different ways:

 read()

 Read from the cursor to the end of the file, return the contents as a String

 read(5)

 Read the next 5 characters starting from the cursor (and move the cursor)

 readlines()

 Read from the cursor to the end of the file, return the contents as a list of String, where

each String is a single line in the file (splits the text up by line).

 readline()

 Read from the cursor up to and including the next line break character. Returns a string.

slide-4
SLIDE 4

Other file I/O functions

 seek(character_number)

 Seek to the character_number-th character, where the first character in the file is

the zero-th character

 seek(0) – Rewind the cursor to the beginning of the file

 close()

 Close the file reader/writer  When you no longer need the file, you should ALWAYS close it

 File I/O is memory expensive  Writes won’t necessarily be committed until you close()

slide-5
SLIDE 5

Write/append mode

 write(some_text)

 Writes text to a file.  There is no write line, so add “\n” to add a new line character

 Always remember to close()

 When you call close, this will forcibly update the file

 If you want to read the file and write to it, use w+, a+

slide-6
SLIDE 6

Reading from the internet

import urllib.request url = some-url-to-the-file stream = urllib.request.urlopen( url) for line in stream: decoded = line.decode("UTF-8") print(decoded.strip())

Note that this reads the file in HTML format. As needed, we’ll explain how to extract the contents from the HTML later

slide-7
SLIDE 7

Reading a CSV File

 CSV – Comma separate values  CSV File Example: John,Doe,120 jefferson st.,Riverside, NJ, 08075 Jack,McGinnis,220 hobo Av.,Phila, PA,09119 "John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075 Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234, ,Blankman,,SomeTown, SD, 00298 "Joan ""the bone"", Anne",Jet,9 Terrace Place, Desert City,CO,00123

https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv

slide-8
SLIDE 8

Think of that file like this:

First Name Last Name Address City State Zip John Doe 120 Jefferson St. Philadelphia PA 08075 Jack McGinnis 220 Hall Ave. Philadelphia PA 09119 John “Da Man” Repici 120 Jefferson St. Riverside NJ 08075 Stephen Tyler 7452 Terrace “At the Plaza” Road Sometown SD 91234 Blankman Sometown SD 00298 Joan “the bone” Anne Jet 9 Terrace Place Desert City CO 00123 https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv

slide-9
SLIDE 9

Reading a CSV File

 Simple use:

  • pen()

readlines() or readline() your choice split(“,”) strip() is also useful to remove white space

 Use this to convert each line to a list to extract what you want.

slide-10
SLIDE 10

In-Class Exercise

 Download the CSV from this website:

https://people.sc.fsu.edu/~jburkardt/data/csv/deniro.csv Do this either within Java itself, or download the file manually and put it in your project.

 Answer the following (with code):

 How many Robert Deniro movies are awful (< 25 on Rotten Tomatoes)  What is Robert Deniro’s best movie  What is Robert Deniro’s average Rotten Tomatoes score per decade?