file i o with python file opening
play

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,


  1. File I/O With Python

  2. 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, consectetur amet, consectetur adipiscing elit. Ut adipiscing elit. Ut adipiscing elit. Ut sollicitudin bibendum odio, sollicitudin bibendum odio, sollicitudin bibendum odio, non aliquet est viverra vel. non aliquet est viverra vel. non aliquet est viverra vel. Sed auctor tempor massa, Sed auctor tempor massa, Sed auctor tempor massa, at molestie felis eleifend at molestie felis eleifend at molestie felis eleifend ac. ac. [Starting writing to ac. file here] [ignore existing contents, [you cannot edit the file, write file from the only read from it] beginning]

  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.

  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()

  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+

  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

  7. Reading a CSV File https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv  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

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

  9. Reading a CSV File  Simple use: open() 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.

  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?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend