File I/O With Python File Opening file_reading_variable = - - PowerPoint PPT Presentation
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,
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]
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.
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()
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+
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
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
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
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.
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?