Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
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 2016 Lecture 27: Sets and Dictionaries
Chapter 7 Text Input/Output Objectives
Adapted from slides by Marty Stepp and Stuart Reges
Hours question Given a file hours.txt with the following contents:
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSC 108H: Introduction to Computer Programming
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
Building Java Programs
Binary Files.
Adapted from slides by Marty Stepp and Stuart Reges
while loops; file I/O; introduction to lists
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Why did the programmer quit his job?
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Reading and Writing Files
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
CSc 110, Spring 2018 Lecture 10: More Graphics
CSc 110, Autumn 2016 Programming feel like that?
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 8: More Graphics
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
A counting problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2017 Lecture 25: File output Adapted from slides by Marty Stepp and Stuart Reges 1

Section attendance question Read a file of section attendance (see next slide): yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny And produce the following output: Section 1 Student points: [20, 16, 17, 14, 11] Student grades: [100.0, 80.0, 85.0, 70.0, 55.0] Section 2 Student points: [16, 19, 14, 14, 8] Student grades: [80.0, 95.0, 70.0, 70.0, 40.0] Section 3 Student points: [16, 15, 16, 18, 14] Student grades: [80.0, 75.0, 80.0, 90.0, 70.0] Students earn 3 points for each section attended up to 20.

Section input file Each line represents a section. student 123451234512345123451234512345123451234512345 Each line represents a section. A line consists of 9 weeks' worth of data. Each week has 5 characters because there are 5 students. Within each week, each character represents one student. a means the student was absent (+0 points) n means they attended but didn't do the problems (+1 points) y means they attended and did the problems (+3 points) week 1 2 3 4 5 6 7 8 9 section 1 section 2 section 3 yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny

Output to files Open a file in write or append mode 'w' - write mode – replaces everything in the file 'a' – append mode – adds to the bottom of the file preserving what is already in it name = open("filename", "w") # write name = open("filename", "a") # append

Output to files name.write(str) - writes the given string to the file name.close() - closes file once writing is done Example: out = open("output.txt", "w") out.write("Hello, world!\n") out.write("How are you?") out.close() text = open("output.txt").read() # Hello, world!\nHow are you? common PrintStream bug: - declaring it in a method that gets called many times. This causes the file to be re-opened and wipes the past contents. So only the last line shows up in the file.