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.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
While loops.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Files in Python Caution about readlines vs. read and split.
Topics This week: File input and output Python Programming, 2/e 1.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Comments are for people Header comments supply basic information about the artifact.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
For. for loop The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
For loops in programming Assumes you have seen assignment statements and print statements.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
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])
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Program Input and the Software Design Process ROBERT REAVES.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Nested Loops CS303E: Elements of Computers and Programming.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
File I/O File input/output Iterate through a file using for
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Strings Part 1 Taken from notes by Dr. Neil Moore
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lists in Python.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Using files Taken from notes by Dr. Neil Moore
File I/O File input/output Iterate through a file using for
Strings and Lists – the split method
Lists in Python Creating lists.
CS 1111 Introduction to Programming Fall 2018
CS190/295 Programming in Python for Life Sciences: Lecture 3
CISC101 Reminders All assignments are now posted.
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Python Simple file reading
CS 1111 Introduction to Programming Spring 2019
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Computer Science
String Objects & its Methods
Presentation transcript:

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 of reading that I call “bulk reads” because with one statement they totally exhaust the file. There is no more to read after that! The other two ways read a line at a time from the file Files are objects so most of these will be methods called with the dot notation as usual

read() The read method is called like this datastr = infile.read() What does it do? it reads in the entire file of data, into one string variable The newlines and other whitespace in the file are stored in the string like every other character Be aware if you are reading a LARGE file, this may take some time and a lot of RAM! This is convenient if you do not care particularly where the newlines are in the file BULK

readlines() The syntax: datalst = infile.readlines() This method reads in ALL the data from the file and uses the \n as a delimiter to break the data into strings in a list There is nothing more to read in the file after you execute one readlines call. This is convenient if you know the data in the file is organized by lines, i.e. each line needs to be processed by itself BULK

readline() Note that this is a different method from readlines – note the s! syntax: datastr = infile.readline() Semantics: it reads in the next line of data from the file, up to the next newline Returns a string which has the data and a \n character at the end Useful when you don’t want to read in ALL the data at one time, or when you have more data than RAM space to hold it Usually used inside a while loop Indicates the end of the data in the file by returning an empty string. Note that this is different from having an empty or blank line in the file – that is returned as “\n”

for line in infile This is a method unique to Python The for loop itself reads in one line from the file connected to infile and stores it in the variable of the for loop, as a string with a \n on the end The loop automatically stops when the file is exhausted You have to process the line inside the loop – when the loop iterates, a new line is read in Useful when you don’t have enough RAM to hold the whole file or when you don’t want to read all the data at one time