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

Slides:



Advertisements
Similar presentations
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Advertisements

Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Strings, if/else, return, user input
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
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.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python for Informatics: Exploring Information
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.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Exploration Seminar 4 Basics Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck and Roy McElmurry for their work on these slides. Except where otherwise.
File I/O CMSC 201. Overview Today we’ll be going over: String methods File I/O.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
If/else, return, user input, strings
Strings, if/else, user input
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
CMSC201 Computer Science I for Majors Lecture 11 – File I/O (Continued) Prof. Katherine Gibson Based on concepts from:
Prof. Katherine Gibson Prof. Jeremy Dixon
strings, if/else, user input
27-Jan-1827-Jan-1827-Jan-1827-Jan-1827-Jan-1827-Jan-1827-Jan-1827-Jan-1827-Jan-18 Introduction.
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Higher Order Functions
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Writing & reading txt files with python 3
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
String Processing Upsorn Praphamontripong CS 1110
CSc 120 Introduction to Computer Programing II
Last Class We Covered File I/O How to open a file
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
review; file processing
Week 1 Review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
while loops; file I/O; introduction to lists
CSc 110, Autumn 2016 Lecture 16: File Input.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
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 17: Line-Based File Input
expressions, variables, for loops
Chapter 8 More on Strings and Special Methods
expressions, variables, for loops
while loops; logic; random numbers; tuples
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
Reading and Writing Files
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
Lambda Functions, MapReduce and List Comprehensions
CS 1111 Introduction to Programming Spring 2019
Last Class We Covered File I/O How to open a file
Topics Introduction to File Input and Output
Adapted from slides by Marty Stepp and Stuart Reges
IST256 : Applications Programming for Information Systems
Presentation transcript:

Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this work is licensed under:

2 Reading Files name = open(" filename ") –opens the given file for reading, and returns a file object name.read()- file's entire contents as a string name.readline()- next line from file as a string name.readlines()- file's contents as a list of lines –the lines from a file object can also be read using a for loop >>> f = open("hours.txt") >>> f.read() '123 Susan \n 456 Brad \n 789 Jenn \n'

3 File Input Template A template for reading files in Python: name = open(" filename ") for line in name : statements >>> input = open("hours.txt") >>> for line in input:... print(line.strip()) # strip() removes \n 123 Susan Brad Jenn

4 Exercise Write a function input_stats that accepts a file name as a parameter and that reports the longest line in the file. –example input file, carroll.txt : Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. –expected output: >>> input_stats("carroll.txt") longest line = 42 characters the jaws that bite, the claws that catch,

5 Exercise Solution def input_stats(filename): input = open(filename) longest = "" for line in input: if len(line) > len(longest): longest = line print("Longest line =", len(longest)) print(longest)

6 Recall: String Methods >>> name = "Martin Douglas Stepp" >>> name.upper() 'MARTIN DOUGLAS STEPP' >>> name.lower().startswith("martin") True >>> len(name) 20 JavaPython length len( str ) startsWith, endsWithstartswith, endswith toLowerCase, toUpperCase upper, lower, isupper, islower, capitalize, swapcase indexOffind trimstrip ord, chr

7 String Splitting split breaks a string into tokens that you can loop over. name.split()# break by whitespace name.split( delimiter )# break by delimiter join performs the opposite of a split delimiter.join( list of tokens ) >>> name = "Brave Sir Robin" >>> for word in name.split():... print(word) Brave Sir Robin >>> "LL".join(name.split("r")) 'BLLave SiLL Robin

8 Splitting into Variables If you know the number of tokens, you can split them directly into a sequence of variables. var1, var2,..., varN = string.split() may want to convert type of some tokens: type ( value ) >>> s = "Jessica " >>> name, age, money = s.split() >>> name 'Jessica' >>> int(age) 31 >>> float(money)

9 Exercise Suppose we have this hours.txt data: 123 Suzy Brad Jenn Compute each worker's total hours and hours/day. –Assume each worker works exactly five days. Suzy ID 123 worked 31.4 hours: 6.3 / day Brad ID 456 worked 36.8 hours: 7.36 / day Jenn ID 789 worked 39.5 hours: 7.9 / day

10 Exercise Answer hours.py input = open("hours.txt") for line in input: id, name, mon, tue, wed, thu, fri = line.split() # cumulative sum of this employee's hours hours = float(mon) + float(tue) + float(wed) + \ float(thu) + float(fri) print(name, "ID", id, "worked", \ hours, "hours: ", hours/5, "/ day"

11 Writing Files name = open(" filename ", "w") name = open(" filename ", "a") –opens file for write(deletes previous contents), or –opens file for append(new data goes after previous data) name.write( str )- writes the given string to the file name.close()- saves file once writing is done >>> out = open("output.txt", "w") >>> out.write("Hello, world!\n") >>> out.write("How are you?") >>> out.close() >>> open("output.txt").read() 'Hello, world!\nHow are you?'

12 Exercise Write code to read a file of gas prices in USA and Belgium: /21/ /28/ /4/11... Output the average gas price for each country to an output file named gasout.txt.