ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 6 - File IO



Similar documents
ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Python Lists and Loops

Introduction to: Computers & Programming: Input and Output (IO)

SnapLogic Tutorials Document Release: October 2013 SnapLogic, Inc. 2 West 5th Ave, Fourth Floor San Mateo, California U.S.A.

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

TECHNICAL REFERENCE GUIDE

Time Clock Import Setup & Use

Import Clients. Importing Clients Into STX

EXCEL IMPORT user guide

Moving from CS 61A Scheme to CS 61B Java

RA MODEL VISUALIZATION WITH MICROSOFT EXCEL 2013 AND GEPHI

LabVIEW Report Generation Toolkit for Microsoft Office User Guide

SPSS for Windows importing and exporting data

Product: DQ Order Manager Release Notes

User Manual - Sales Lead Tracking Software

TECHNICAL REFERENCE GUIDE

Welcome to GAMS 1. Jesper Jensen TECA TRAINING ApS This version: September 2006

Importing Data from a Dat or Text File into SPSS

FreeForm Designer. Phone: Fax: POB 8792, Natanya, Israel Document2

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Adding a DNS Update Step to a Recovery Plan VMware vcenter Site Recovery Manager 4.0 and later

JAVASCRIPT AND COOKIES

TIBCO Spotfire Automation Services 6.5. User s Manual

CRASH COURSE PYTHON. Het begint met een idee

Reading Input From A File

Microsoft Office. Mail Merge in Microsoft Word

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Getting your data into R

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

TECHNICAL REFERENCE GUIDE

MySQL for Beginners Ed 3

SelectSurvey.NET User Manual

SA-9600 Surface Area Software Manual

Importing Data into R

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Prescribed Specialised Services 2015/16 Shadow Monitoring Tool

enicq 5 External Data Interface User s Guide

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Home Loan Manager Pro 7.1

Exercise 1: Python Language Basics

PYTHON Basics

IceWarp to IceWarp Server Migration

Storing Measurement Data

Excel for Mac Text Functions

NDSR Utilities. Creating Backup Files. Chapter 9

3 IDE (Integrated Development Environment)

Computer Programming C++ Classes and Objects 15 th Lecture

HDFS. Hadoop Distributed File System

NØGSG DMR Contact Manager

CPM release notes

Simple File Input & Output

Module 816. File Management in C. M. Campbell 1993 Deakin University

Python Programming: An Introduction to Computer Science

Version 1.5 Satlantic Inc.

A Brief Introduction to MySQL

ESPResSo Summer School 2012

Data Integrator. Pervasive Software, Inc B Riata Trace Parkway Austin, Texas USA

Let SAS Do the Downloading: Using Macros to Generate FTP Script Files Arthur Furnia, Federal Aviation Administration, Washington DC

Chapter 12 File Management. Roadmap

Sources: On the Web: Slides will be available on:

Chapter 12 File Management

Impala Introduction. By: Matthew Bollinger

SPSS The Basics. Jennifer Thach RHS Assessment Office March 3 rd, 2014

Object Oriented Software Design

UTILITIES BACKUP. Figure 25-1 Backup & Reindex utilities on the Main Menu

Basics Series-4004 Database Manager and Import Version 9.0

Reading and writing files

DASYLab Techniques. Saving DASYLab data to an ASCII (text) readable file. Updated to reflect changes in DASYLab 13

6. Control Structures

PHP Tutorial From beginner to master

6.170 Tutorial 3 - Ruby Basics

Introduction to Python for Text Analysis

Converting an SPSS Data File to Mplus. by Paul F. Tremblay September 2013

SQL2report User Manual

Programming in Python V: Accessing a Database

Using Style Sheets for Consistency

Chapter 2 The Data Table. Chapter Table of Contents

#include <Gamer.h> Gamer gamer; void setup() { gamer.begin(); } void loop() {

Generating a Custom Bill of Materials

USING MAGENTO TRANSLATION TOOLS

Creating Raw Data Files Using SAS. Transcript

LabVIEW Report Generation Toolkit for Microsoft Office

Visualization with Excel Tools and Microsoft Azure

Business Insight Report Authoring Getting Started Guide

Data Tool Platform SQL Development Tools

Excel: Introduction to Formulas

Utility Classes - Overview. Version 14 service pack 9. Dalestech Ltd. Page 1 of 23

Custom Reporting System User Guide

Vendor: Brio Software Product: Brio Performance Suite

WS_FTP Professional 12

Data Presentation. Paper Using SAS Macros to Create Automated Excel Reports Containing Tables, Charts and Graphs

Oracle 10g PL/SQL Training

Data Integrator. Samples. Handbook. Pervasive Software, Inc B Riata Trace Parkway Austin, Texas USA

Research Electronic Data Capture Prepared by Angela Juan

Writing Scripts with PHP s PEAR DB Module

AP Computer Science Java Mr. Clausen Program 9A, 9B

Office of History. Using Code ZH Document Management System

Transcription:

ESCI 386 Scientific Programming, Analysis and Visualization with Python Lesson 6 - File IO 1

Opening/Closing Files A file is opened for reading using the open statement f = open(file_name, r ) This returns a file object, in this case named f. We could have named it whatever we wanted. The r specifies the mode of the file be read only. The different possible modes are Mode Meaning Mode Meaning r Read only (text file) rb Read only (binary file) w Write only (text file) wb Write only (binary file) r+ or w+ Read and write (text file) rb+ or wb+ Read and write (binary file) a Append (text file) ab Append (binary file) 2

Opening/Closing Files An ASCII file is opened for reading using the open statement f = open(file_name, r ) This returns a file object, in this case named f. We could have named it whatever we wanted. The r specifies the mode of the file be read only. To open a file for writing we would use w instead of r. Opening a file with a will open the file for writing and append data to the end of the file. To open a file for both reading and writing we use either r+ or w+. You should always close a file when you are done with it. This is done with f.close() 3

Automatically Closing Files Python has a shorthand for opening a file, manipulating its contents, and then automatically closing it. The syntax is with open(filename, r ) as f: [code statements within code block] This opens the file and gives the file object the name f. The file will automatically close when the code block completes, without having to explicitely close it. 4

Interactive File Selection The code below allows interactive selection of file names. import Tkinter as tk from tkfiledialog import askopenfilename as pickfile window = tk.tk() # Create a window window.withdraw() # Hide the window filename = pickfile(multiple=false) window.quit() # quit the window 5

Interactive File Selection (cont.) The full path and name of the selected file will be stored as a string in the variable filename, which can then be used to open and manipulate the file. Even on Windows machines the path names will use Linux style forward slashes / rather than Windows style back slashes \. Multiple file names can also be selected by setting the multiple keyword to True. If multiple files are selected then filename will be a single string containing all the filenames separated by white space. On Windows machines where files themselves may have white spaces, if any of the filenames have white spaces within them then their filenames are contained in curly braces. 6

Interactive Directory Selection Directories can also be selected interactively. import Tkinter as tk from tkfiledialog import askdirectory as pickdir window = tk.tk() # Create a window window.withdraw() # Hide the window dirname = pickdir(mustexist=true) window.quit() # quit the window 7

Reading from Text Files The simplest way to sequentially read all the lines in a file an manipulate them is to simply iterate over the file object. For example, to read the lines of a file and print them to the screen we would use f = open('datafile.txt', 'r') for line in f: print(line) 8

Reading from Text Files We can also read a single line at a time using the readline() method. line = f.readline() If we want to read all the lines in file and place them into a list, with each line being a separate element of the list, we can use the readlines() method lines = f.readlines() lines[0] would then contain the entire first line of the file as a string, lines[1] would contain the next line, etc. 9

Writing Text to Files Writing to text files is accomplished with the write() or writelines() methods. The write() method writes a string to the file. The writelines() method writes a list of strings to file. 10

Writing Text to Files (Example) f = open('myfile.txt', 'w') f.write('the quick brown fox') f.write(' jumped over the\n') f.write('lazy dog.') f.close() Results in the contents of myfile.txt The quick brown fox jumped over the lazy dog. 11

Writing Text to Files Both the write() and writelines() methods require strings as the input data type. To write numerical data to a file using these methods you first have to convert them to strings. 12

Reading and Writing CSV Files In many data files the values are separated by commas, and these files are known as commaseparated values files, or CSV files. Python includes a csv module that has functions and methods for easily reading and writing CSV files. 13

Reading and Writing CSV Files To read CSV file titled myfile.csv we first open the file as usual, and then create an instance of a reader object. The reader object is an iterable object, that can be iterated over the lines in the file. IMPORTANT: When opening a file for csv reader or writer, it should be opened as a binary file, using either rb or wb as the mode. This prevent an extra blank lines being inserted in the output file. 14

Example Reading CSV File import csv f = open('myfile.csv', 'rb') # NOTE: Used 'rb' r = csv.reader(f) # creates reader object for i, line in enumerate(r): if i == 0: continue # skips header row n, rho, mass = line volume = float(mass)/float(rho) print(n, volume) f.close() 15

Example Writing CSV File import csv f = open('myfile-write.csv', 'wb') # NOTE: Used 'wb' w = csv.writer(f) data = [['Element', 'Atomic Number', 'Molar Mass'],\ ['Helium', '1', '1.008'],\ ['Aluminum', '13', '26.98']] w.writerows(data) f.close() 16

The csv Module is Flexible The csv module can be used with delimiters other than commas. To do this, we set the delimiter keyword as shown below: r = csv.reader(f, delimiter = ; ) # semicolon delimited file r = csv.reader(f, delimiter = ) # space delimited file 17

Using the with Statement The with statement is a shorthand way of always making certain that your files are closed when you are done with them. The with statement is used as follows with open(filename, r ) as f: code block that uses the file object f When the code block is exited, the file object f will be automatically closed 18

with Statement Example filename = my_input_file f = open(filename, r ): data = f.readlines() f.close() for line in data: print(line) File closure is automatic using the with statement filename = my_input_file with open(filename, r ) as f: data = f.readlines() for line in data: print(line) 19

Writing and Reading Numpy Arrays to Text Files Numpy Arrays can be written to text files using a.tofile() numpy.savetxt() Numpy Arrays can be read from text files using numpy.fromfile() numpy.loadtxt() numpy.genfromtxt() Can write headers/footers Can skip headers, specify columns Very basic Most flexible. Handles missing data. Skip footers and headers. Much more. These methods can also read/write binary files, but they won t be machine portable. 20

Examples of Reading Data On the next slide we show four example programs for reading numerical data from a text file of the form shown below, with a header row and commas separating values. Gender, Weight (lbs) Male, 171.0 Male, 179.6 Male, 174.7 Male, 172.5 Female, 161.4 Male, 192.7 Male, 190.4 Male, 193.4 21

Examples of Reading Data Iterating over file object filein = 'weight-data.txt' weight = [] # empty list to hold data with open(filein, 'r') as f: for i, line in enumerate(f): if i == 0: pass # skips header else: data = line.split(',') # Splits line weight.append(float(data[-1])) filein = 'weight-data.txt' weight = [] # empty list to hold data with open(filein, 'r') as f: file_data = f.readlines() for line in file_data[1:]: # Skips head data = line.split(',') # Splits line weight.append(float(data[-1]) print(weight) Using readlines() print(weight) 22

Examples of Reading Data import csv filein = 'weight-data.txt' weight = [] # empty list with open(filein, 'rb') as f: w = csv.reader(f, delimiter = ',') for i, line in enumerate(w): if i == 0: pass # Skip first row else: weight.append(float(line[-1])) print(weight) Using CSV Reader import numpy as np filein = 'weight-data.txt' # Use loadtxt skipping 1 row and # only using second column weight = np.loadtxt(filein, dtype = np.float_, delimiter = ',', skiprows = 1, usecols = (1,)) print(weight) Using numpy.loadtxt() Note comma (needed if only one column is used) 23

.npy and.npz files NumPy provides its own functions to read and write arrays to binary files. This is accomplished with either: np.save() function, which writes a single array to a NumPy.npy file. np.savez() function, which archives several arrays into a NumPy.npz file. 24

import numpy as np a = np.arange(0, 100)*0.5 b = np.arange(-100, 0)*0.5 np.save('a-file', a) np.save('b-file', b) np.savez('ab-file', a=a, b=b) Example Creates three files: a-file.npy which contains the values for a b-file.npy which contains the values for b ab-file.npz which is an archive file containing both the a and b values 25

Loading.npy Files To retrieve the values from the.npy files we use the np.load() function a = np.load('a-file.npy') b = np.load('b-file.npy') 26

Loading.npz Files To retrieve the values from the.npz files we also use the np.load() function to load all the data into a dictionary that contains the archived arrays. z = np.load('ab-file.npz') a = z[ a ] b = z[ b ] 27

Loading.npz Files To find the names of the arrays used in the dictionary, use the files attribute of the dictionary >>> z.files [ a, b ] 28

Working with Pathnames The os.path module contains some nice functions for manipulating path and file names. I usually import os.path aliased to pth import os.path as pth 29

Some os.path Functions >>> import os.path as pth >>> p = 'C:\\data\\temperature\\jun11.dat' >>> pth.abspath(p) 'C:\\data\\temperature\\jun11.dat' >>> pth.basename(p) 'jun11.dat' >>> pth.dirname(p) 'C:\\data\\temperature 30

Some os.path Functions >>> pth.exists(p) False >>> pth.isfile(p) False >>> pth.isdir(p) False 31

Some os.path Functions >>> pth.split(p) ('C:\\data\\temperature', 'jun11.dat') >>> pth.splitdrive(p) ('C:', '\\data\\temperature\\jun11.dat') >>> pth.splitext(p) ('C:\\data\\temperature\\jun11', '.dat') 32