Files in Python Caution about readlines vs. read and split.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Getting Input in Python Assumes assignment statement, typecasts.
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
User Input. Why user entry? When we assign values (age = 16), we are making a program static…it never changes. If we ask the user to enter the values,
Files Introduction to Computing Science and Programming I.
© 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 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.
Format Scandisk Defragmentation Antivirus Compression Software
Topics This week: File input and output Python Programming, 2/e 1.
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"
Lists in Python.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Data Storage Choices File or Database ? Binary or Text file ? Variable or fixed record length ? Choice of text file record and field delimiters XML anyone.
Course A201: Introduction to Programming 12/9/2010.
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.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Files Tutor: You will need ….
Sequencing The most simple type of program uses sequencing, a set of instructions carried out one after another. Start End Display “Computer” Display “Science”
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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(),
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
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.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
DEPARTMENT MODULE User’s Guide. Step 1. Click Files Step 2. Click Department.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
CIT 590 Intro to Programming Lecture 6. Vote in the doodle poll so we can use some fancy algorithm to pair you up You.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
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.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Computer Programming ||
Introduction to Computing Science and Programming I
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python’s input and output
Strings in Python Creating a string.
Strings Part 2 Taken from notes by Dr. Neil Moore
Chapter 7 Files and Exceptions
Using files Taken from notes by Dr. Neil Moore
Week 9 – Lesson 1 Arrays – Character Strings
Strings and Lists – the split method
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Creating lists.
CS 1111 Introduction to Programming Fall 2018
Class Examples.
CS190/295 Programming in Python for Life Sciences: Lecture 3
LING 408/508: Computational Techniques for Linguists
Topics Introduction to File Input and Output
Python 16 Mr. Husch.
File Input and Output.
Understanding Variables
CSE 231 Lab 6.
Python 16 Mr. Husch.
Copyright (c) 2017 by Dr. E. Horvath
Nate Brunelle Today: Web Reading
CS 1111 Introduction to Programming Spring 2019
Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Computer Science
Presentation transcript:

Files in Python Caution about readlines vs. read and split

You would think that lines = infile.readlines() and line = infile.read() lines = line.split(‘\n’) would give the same result in the variable lines, that is, a list of strings from the file, delimited by the newline characters.

You would be surprised! readlines() gives you a list of strings, each with a \n at the end Except! if you did not press Enter on the last line of the data file, the last string in the list will not have a \n in it read() followed by split(‘\n’) gives a list of strings, yes, but none of them will have \n in them (remember split removes the delimiters from its results)

And another surprise! If you did press Enter on the last line of the data file, readlines still works properly. The last string in the list will have a \n character just like all the others BUT the same file read with the read/split combination will have one extra entry, an empty string at the end of the list This is something you need to be aware of while processing your data – many programs crash because they assume that every string will be the same length, for example.