Files and Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.

Slides:



Advertisements
Similar presentations
Statistical text mining using R Tom Liptrot The Christie Hospital.
Advertisements

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
It was the Best of Times, It was the Worst of Times, It was a Colossal Comma Splice British Literature April 13, 2015.
A Tale of Two Cities Introduction
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
Grade 7 module 1 unit 2 lesson
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.
A sharp contrast to the extreme poverty conditions that you read about yesterday.
A Tale of Two Cities A Brief Introduction The “two cities” are Paris, in the time of the French revolution, and London. Dr. Manette, a French physician,
Contours of Inclusion: The Intersection between the Arts and Universal Design for Learning.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Syntax The manner in which a speaker or author constructs a sentence affects what the audience understands … syntax must be examined for its ability to.
FRONT LivijaAlex Y.SpencerAdrianShannonMichael JuliaChadJohnAlex F.SamAndrea CalebThomas D. Thomas B. LuisEthanJoe Will M-MKatieDuncanKeeganChrisWill M.
“Here’s the thing about equality, we’re all equal when we’re dead.”
Predicate The verb and other words about the subject.
A CD of this message will be available (free of charge) immediately following the service. It will also be available for podcast later this week at calvaryokc.com.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
August 20,  Write the following paragraph in cursive. ◦ It was the best of times, it was the worst of times, it was the age of wisdom, it was the.
Parallelism Objective Today, we are going to identify and correct faulty parallel structure in sentences. 1.What are we going to do today? 2.What are.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Literary Devices.
Book I Chapter 1 “The Period”
Files Tutor: You will need ….
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])
Analyzing an Author’s Style Part 1 Diction Pace Tone Figures of Speech.
Examples of Anaphora and Antithesis. Anaphora: repetition of a word or words at the beginning of two or more successive verses, clauses, or sentences.
TEMPLATE. 1 ADD TITLE CONTENT ONE It was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness.
23 OCTOBER 2014 What were some rhetorical devices/stylistic devices that you found in your research last night? Be prepared to share with the class! What.
How the parts of speech form complete thoughts PARTS OF A SENTENCE.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
British Literature April 8, Kaylynn Champion.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
ADVERBS Main definition of an Adverb: like an adjective, an adverb describes an action completely and clearly. Adverbs modify, or tell about, verbs, adjectives,
Alternative Facts The Challenges of Settlements & Emerging Insurance Issues 10th Annual McGill Conference of International Aviation Liability & Insurance.
Containers Demo – Alisson Sol – July/2016
a few important terms to expand your thinking
The Absolutely True Diary of a Part-Time Indian
Formalism a few important terms.
     .
Journal #16: Juxtaposition
Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 1
Python I/O.
Introduction to Python
Writing with Concord: Parallel Structure
town planning I urban design I environmental assessment
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
T E M P L A T E.
T E M P L A T E.
Files Handling In today’s lesson we will look at:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
LOGO XX学校 这回我们毕业啦 其实是答辩的标题地方 我们毕业啦 其实是答辩的标题地方 答辩人 指导老师.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Hello It was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness.
T E M P L A T E.
Repetition Repetition.
Weak & Strong Forms.
Anaphora.
1. Bell Ringer: Independent Reading 2. Writing Practice 3. Lit Terms
Judges A CD of this message will be available (free of charge) immediately following the service. It will also be available for podcast later this.
Presentation transcript:

Files and Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009

Today Working with files A new data type: Dictionaries Top-down design example 2

Files Files can contain... Text Program Source Code def compound_interest(principle, yearly_rate, monthly_payment): print "Year", "Principle" monthly_rate = yearly_rate / 12.0 m = 0 while principle >= 0: print m, principle principle = principle + monthly_rate * principle - monthly_payment m = m + 1 3

Files Files can contain... Text Natural Language It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only. 4

Files Files can contain... Text Web pages 5

Files Files can contain... Text Some kinds of data, e.g.: "CSV" (comma separated value) spreadsheet files year,brand,model,options,price 1997,Ford,E350,"ac, abs, moon", ,Chevy,"Venture "Extended Edition", ,Jeep,Grand Cherokee,"air, moon roof, loaded",

Files Files can contain... Binary Numbers Compiled programs Compressed files (.zip) Binary data files (e.g..xls) Sounds (e.g..mp3) Images (e.g..jpeg) 7

I/O with Text Files Today we will learn how to perform I/O with text files I/O = "Input / Output" In Python, text = strings Reading strings from files Formatting strings Writing strings to files 8

Text Input From the terminal: story = raw_input('Enter your story: ') From a file: Associate a file object with the given file name Read from the file object one or more times Try: dickens = open('two-cities.txt', 'r') dickens.readline() dickens.read() 9

Text Input General form: = open(, ) is 'r' for reading text files.readline() Reads one line from the return, returns that line as a string Next call to readline() will return the next line of file.read() Reads entire file, returns entire text as one long string Special characters '\r' or '\n' (or both) separate lines 10

Text Input Special Python shortcut for iterating over the lines of a file for line in : do something with line Try: [ex1.py] dickens = open('two-cities.txt','r') count = 0 for line in dickens: print line count = count + 1 print "The number of lines is ", count 11

Text Input Why do I call this a shortcut? for line in dickens: print line count = count + 1 is equivalent to writing: line = dickens.readline() while line != "": print line count = count + 1 line = dickens.readline() 12

Text Output Terminal output: print "Hello, world!" File output: Associate a file object with the given file name Write from the file object one or more times Close the file, to tell the computer you are done with it Try: henry = open('mystory.txt', 'w') henry.write('It was the stupidest of times.\n') henry.close() 13

Text Output General form: = open(, ) is 'w' for writing text files if already exists, it is erased.write( ) Writes to (end of) file In, the special notation '\n' stands for a single end of line character.close() Tells the computer you are done using If you leave it out, your file might not be saved 14

Dictionaries Dictionary: like a list, but items are associated with key words rather than with numeric indexes A regular dictionary: cat: a feline pet gnat: an annoying insect hat: a head covering Try: d = { } # creates an empty dictionary d['cat'] = 'a feline pet' d['gnat'] = 'an annoying insect' d['hat'] = 'a head covering' 15

Course Status Read: Zelle, Section 11.6 (Dictionaries) Assignment 3: RNA Secondary Structure Prediction Turn in by 10:00am Saturday Next workshop: measuring literary style Programming assignment 4: will be given out on Tuesday 16