File Processing CMSC 120: Visualizing Information Lecture 4/15/08.

Slides:



Advertisements
Similar presentations
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Advertisements

© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Introduction to Computers and Programming Lecture 7:
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
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.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Topics This week: File input and output Python Programming, 2/e 1.
ASCII & Gray Codes.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Introduction to Computing Using Python Chapter 6  Encoding of String Characters  Randomness and Random Sampling.
Introduction to Human Language Technologies Tomaž Erjavec Karl-Franzens-Universität Graz Tomaž Erjavec Lecture: Character sets
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
Chapter 3 Representing Numbers and Text in Binary Information Technology in Theory By Pelin Aksoy and Laura DeNardis.
Representing text Each of different symbol on the text (alphabet letter) is assigned a unique bit patterns the text is then representing as.
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Aloha Aloha What you see: What the computer sees: binary number columns binary number columns
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12.
 The amount of data we deal with is getting larger  Not only do larger files require more disk space, they take longer to transmit  Many times files.
Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files.
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 18 Using text files to share data with other programs 5/07/09 Python Mini-Course:
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Input and Output CMSC 120: Visualizing Information Lecture 4/10.
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])
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.
File Processing Recap. String Processing: Review Escape Sequences: print the unprintable '\ ' : escape sequence '\n' : newline '\t' : tab '\"' : " >>>
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
1 Huffman Codes Computing an Optimal Code for a Document.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Files in Python Opening and Closing. Big Picture To use a file in a programming language – You have to open the file – Then you process the data in the.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Text and Images Key Revision Points.
Topic: Binary Encoding – Part 2
Binary Representation in Text
Binary Representation in Text
Topic: Binary Encoding – Part 1
Chapter 3 Data Representation Text Characters
OO Design and Programming II I/O: Reading and Writing
Module 5 Working with Data
Learning Intention Learning Intention: To develop understanding of variables, data types, and comments in text based programming languages Context: Sequencing.
Representing Information as bit patterns
String Manipulation.
Class 9 Reading and writing to files chr, ord and Unicode
Python Lesson 6 Mr. Kalmes.
Chapter 7 Files and Exceptions
Using files Taken from notes by Dr. Neil Moore
Lecture 9: Radix-64 Tutorial
Digital Encodings.
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
CS190/295 Programming in Python for Life Sciences: Lecture 3
LING 408/508: Computational Techniques for Linguists
functions: argument, return value
15-110: Principles of Computing
CS2911 Week 2, Class 3 Today Return Lab 2 and Quiz 1
Python Inputs Mr. Husch.
Python 8 Mr. Husch.
Lecture 36 – Unit 6 – Under the Hood Binary Encoding – Part 2
Presentation transcript:

File Processing CMSC 120: Visualizing Information Lecture 4/15/08

Files in Python Binary Data are encoded in binary Metadata provides instructions for decoding and encoding file Plain text Data converted to text Text converted to numerical representation Numerical code saved to file One really long string

Multi-Line Processing or inputs a sequence of characters end-of-line marker newline '\n‘ >>> print 'Hello\nWorld' Hello World

Escape Sequences '\n' : newline '\t' : tab '\\' : \ '\'' : ' '\"' : " '\a' : ASCII bell ● '\b' : ASCII backspace '\uXXXX' : chr with UNICODE XXXX

File Processing Open the file Associate the file with a name Manipulate the file Read or Write Close the file Disassociate from Name Files are Objects

Opening Files = open(, ) = name of the file = 'r' or 'w' >>> infile = open('mydata.txt', 'r')

Closing Files.close() infile.close()

Reading from Files Read entire file into a single string.read() Read the next line of the file.readline() Read entire file into a list of lines.readlines()

Reading from a File # read and display # first 5 lines of a file infile = open(someFile, 'r') for i in range(5): line = infile.readline() print line[:-1] infile.close()

Writing to a File Just like using the print statement.write( ) # write two lines to a file outfile = open('mydata.txt', 'w') count = 1 outfile.write('This is the first line\n') count = count + 1 outfile.write('This is line number %d.' % count) outfile.close()