CSc 110, Spring 2017 Lecture 17: Line-Based File Input

Slides:



Advertisements
Similar presentations
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Advertisements

Copyright 2008 by Pearson Education Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Topic 20 more file processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: self-check: #7-11.
1 Reminder: String Scanners A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( );
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3, 6.5.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Spring 2017 Lecture 12: Random Numbers
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Introduction to Programming
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Writing & reading txt files with python 3
Thanks to Atif Memon from UMD for disaster examples
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Functions CIS 40 – Introduction to Programming in Python
Lecture 16: Lists (cont.) and File Input
Line-based file processing
CSc 110, Autumn 2017 Lecture 31: Dictionaries
Topic 20 more file processing
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
while loops; file I/O; introduction to lists
CSc 110, Autumn 2016 Lecture 16: File Input.
Building Java Programs
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
Week 7 Lists Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2018 Lecture 8: input and Parameters
Chapters 6 and 7 Line-Based File Input, Arrays reading: , 7.1
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

CSc 110, Spring 2017 Lecture 17: Line-Based File Input Adapted from slides by Marty Stepp and Stuart Reges

Gas prices question Write a program that reads a file gasprices.txt Format: Belgium $/gal US $/gal date 8.20 3.81 3/21/11 8.08 3.84 3/28/11 ... The program should print the average gas price over all data in the file for both countries: Belgium average: 8.3 $/gal USA average: 3.9 $/gal I don't usually have time to do this program in lecture. It's just here in case I have extra time, or for students to look at later.

Gas prices solution ['8.20\n', '3.81\n', '3/21/11\n', '8.08\n', '3.84\n', '3/28/11\n', …. ] def main(): file = open("gasprices.txt") belgium = 0 usa = 0 lines = file.readlines() for i in range(0, len(lines), 3): belgium = belgium + float(lines[i]) usa = usa + float(lines[i + 1]) count = count + 1 print("Belgium average: " + str(belgium / count) + " $/gal") print("USA average: " + str(usa / count) + " $/gal")

Recall the hours.txt file File hours.txt has the following contents: 123 Brett 12.5 8.1 7.6 3.2 456 Sarina 4.0 11.6 6.5 2.7 12 789 Nick 8.0 8.0 8.0 8.0 7.5 How would we process this file if we wanted to extract just the names?

Processing files After using readlines(), each element of the list is a string Each string is a group of characters separated by spaces We may need a new method…. >>> f = open("hours.txt") >>> f.readlines() ['123 Brett 12.5 8.1 7.6 3.2\n', '456 Sarina 4.0 11.6 6.5 2.7 12\n', '789 Nick 8.0 8.0 8.0 8.0 7.5\n'] >>>

Method for splitting strings split()- a method that splits a string into a list of substrings by default, uses spaces to split the string s = "This is a line of words separated by spaces" s = s.split() for i in range(0, len(s)): print(s[i])

Line-based file processing Use readlines() to read the file Then use split() on each line file = open("<filename>") lines = file.readlines() for single_line in lines: parts = single_line.split() <process the parts of the line>

Hours question Given a file hours.txt with the following contents: 123 Brett 12.5 8.1 7.6 3.2 456 Sarina 4.0 11.6 6.5 2.7 12 789 Nick 8.0 8.0 8.0 8.0 7.5 Consider the task of computing hours worked by each person: Brett (ID#123) worked 31.4 hours (7.85 hours/day) Sarina (ID#456) worked 36.8 hours (7.36 hours/day) Nick (ID#789) worked 39.5 hours (7.90 hours/day)

Hours answer 123 Brett 12.5 8.1 7.6 3.2 456 Sarina 4.0 11.6 6.5 2.7 12 # Processes an employee input file and outputs each employee's hours. def main(): file = open("hours.txt") lines = file.readlines() for single_line in lines: process_employee(single_line) def process_employee(line): parts = line.split() id = parts[0] # e.g. "123" name = parts[1] # e.g. "Brett" sum = 0 count = 0 for i in range(2, len(parts)): sum = sum + float(parts[i]) count = count + 1 average = sum / count print(name + " (ID#" + id + ") worked " + str(sum) + " hours (" + str(average) + " hours/day)") 123 Brett 12.5 8.1 7.6 3.2 456 Sarina 4.0 11.6 6.5 2.7 12 789 Nick 8.0 8.0 8.0 8.0 7.5

IMDb movies problem Consider the following Internet Movie Database (IMDb) data: 1 9.1 196376 The Shawshank Redemption (1994) 2 9.0 139085 The Godfather: Part II (1974) 3 8.8 81507 Casablanca (1942) Write a program that displays any movies who titles contain some specified text. It will also display the number of matches found. Search for: part Rank Votes Rating Title 2 139085 9.0 The Godfather: Part II (1974) 40 129172 8.5 The Departed (2006) 95 20401 8.2 The Apartment (1960) 192 30587 8.0 Spartacus (1960) . . . 4 matches

Pseudocode ask the user for the search word open the IMDb data file create a list of the files contents print the header of the output for each line in the list of contents if the search word is in the line increment a matches counter print the line in the proper format print the number of matches Problem: What if there are no matches? Solution: Create a function to search for the word in a file What else should be in a function? 1 9.1 196376 The Shawshank Redemption (1994) 2 9.0 139085 The Godfather: Part II (1974) 3 8.8 81507 Casablanca (1942) Rank Votes Rating Title 2 139085 9.0 The Godfather: …

Pseudocode ask the user for a search word open the IMDb data file create a list of the files contents if search word is in the list of files contents print the header for the output set matches counter for each line in the list if the search word is in the line increment the matches counter print the line in the proper format print the number of matches 1 9.1 196376 The Shawshank Redemption (1994) 2 9.0 139085 The Godfather: Part II (1974) 3 8.8 81507 Casablanca (1942) Rank Votes Rating Title 2 139085 9.0 The Godfather: …

Better IMDb code # Displays IMDB's Top 250 movies that match a search string. def main(): search_word = get_phrase() file = open("imdb.txt") line_list = file.readlines() line = search_list(line_list, search_word) if (len(line) > 0): print("Rank\tVotes\tRating\tTitle") matches = 0 for a_line in line_list: ans = search_line(a_line, search_work) if (len(ans) > 0): matches = matches + 1 display(a_line) print(str(matches) + " matches.") # Asks the user for their search word and returns it. def get_phrase(): search_word = input("Search word: ") search_word = search_word.lower() print() return search_word ...

Better IMDb functions ... # Breaks apart each line, looking for lines that match the search word. def search_list(line_list, search_word): for line in line_list: line_lower = line.lower() # case-insensitive match if (search_word in line_lower): return line return "" # not found # Looks for the search word in a single line. def search_line(line, search_word): # displays the line in the proper format on the screen. def display(line): parts = line.split() rank = parts[0] rating = parts[1] votes = parts[2] title = "" for i in range(3, len(parts)): title += parts[i] + " " # the rest of the line print(rank + "\t" + votes + "\t" + rating + "\t" + title)