1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
UNIT 12 UNIX I/O Redirection.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Basic Input/Output and Variables Ethan Cerami New York
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
1 CSC 221: Introduction to Programming Fall 2011 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 CSC 221: Introduction to Programming Fall 2012 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
1 CSC 221: Introduction to Programming Fall 2013 Big data  building lists list comprehensions, throwaway comprehensions conditional comprehensions  processing.
Python Functions.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Files Tutor: You will need ….
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
1 Huffman Codes Computing an Optimal Code for a Document.
Course A201: Introduction to Programming 09/09/2010.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
Intelligent Data Systems Lab. Department of Computer Science & Engineering Practices 컴퓨터의 개념 및 실습 4 월 11 일.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
File Processing Upsorn Praphamontripong CS 1110
Topic: File Input/Output (I/O)
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Containers and Lists CIS 40 – Introduction to Programming in Python
CS1010 Programming Methodology
CSC 108H: Introduction to Computer Programming
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Encryption and Decryption
CSC 221: Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Fall 2018
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Fundamentals of Python: First Programs
Topics Introduction to File Input and Output
Introduction to Computer Science
Chapter 17 JavaScript Arrays
Topics Introduction to File Input and Output
CSC 221: Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Spring 2019
Introduction to Computer Science
Presentation transcript:

1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(), read(#), readline()  print, write

User interaction so far, all information needed by a function has been passed as inputs  e.g., Caesar cipher had string to be encoded as input >>> caesar("ettubrute")  e.g., hailstone sequence had starting number as input >>> hailstone(5) this was reasonable when input sizes were small  with text processing, may now want to enter lots of text alternatively, could have the function prompt the for the input value 2

User input via input the input function reads a value in from the user input(PROMPT_MESSAGE)  when called, the (optional) prompt message is displayed  the value entered by the user is returned by the function 3 is this better than the old version?

Better design most would agree that the first solution is better  more general – could call the function in a variety of applications  doesn't matter where the string came from  since the value is returned, can be used in different settings better solution  if we really want to utilize user input, keep general-purpose version  call it from another function that does the input.output 4

raw_input entering text with quotes can be tedious  a alternative input function does not require the quotes (the quotes are implicit) raw_input(PROMPT_MESSAGE) 5

Inputting numbers input and raw_input can read in numbers as well  since the hailstone function prints its results, reading the initial value seems reasonable 6

Raw numbers have to be careful reading numbers with raw_input  raw_input always returns a string (assumes implicit quotes are the input) 7 can use int() to convert the string into a number value

Reading from a file for larger amounts of input, more convenient to enter into a file and read directly from the file 8 open(filename, "r") opens the file named filename for input  returns a file object the read() method reads the entire contents of the file object into a string  returns that string the close() method closes the file  should call when done reading

read() vs. read(#) vs. readline() read has an optional input, the number of characters to read  e.g., infile.read(1) reads a single character  returns empty string if at end of file recall that each call to print displays a new line  could print char-by-char using the stdout file object (from sys.py )  the write method does not skip lines 9 readline reads an entire line of text  returns empty string if at end of file

10 Example: file stats suppose we want to generate stats on a file  # of lines  # of characters  average # of characters per line which type of read do we need?

output files here, three different versions of a function that copies a file  open an output file using "w" as the 2 nd input  can write to an output file object using the write method 11

Example: encoding a file could extend our encryption algorithms to files  instead of requiring the user to enter each string, read in from a file  could save encoded/decoded text in a different file  or, could overwrite the original file by using it for both "r" and "w" 12

Browsing for files the open function assumes a relative file address  will look for the filename in the same directory as the program  can specify a path prefix, relative to the current directory infile = open("../Data/"+filename, "r") outfile = open("csc221/Results/"+filename, "w") if you want to be able to search for files, there is a module for that from tkFileDialog import askopenfilename, asksaveasfilename inputfilename = askopenfilename() outputfilename = asksaveasfilename() 13

File dialogs 14

Exercise: pig latin dictionary on the class code page is a dictionary of ~75K words: words.txt you are to download this file, and generate a corresponding pig latin dictionary  the dictionary has one word per line  you may ignore bad results due to spaces or capitalization CAREFUL:  the readline() method (for file objects) returns the entire line of text, including end-of-line characters  the strip() method (for strings) can be called to remove leading and trailing whitespace  but if you strip the end-of-line characters off, successive writes will put everything on one line  you can write "\n" to print a line break in the file 15