- •Introduction
- •Introduction to Python and its use in science
- •Launching Python
- •Installing Python on your computer
- •The Canopy window
- •The Interactive Python Pane
- •Interactive Python as a calculator
- •Python Modules
- •Variables
- •Importing Modules
- •Getting help: documentation in IPython
- •Programming is a detail-oriented activity
- •Exercises
- •Strings, Lists, Arrays, and Dictionaries
- •Strings
- •Lists
- •NumPy arrays
- •Dictionaries
- •Random numbers
- •Exercises
- •Input and Output
- •Keyboard input
- •Screen output
- •File input
- •File output
- •Exercises
- •Plotting
- •An interactive session with pyplot
- •Basic plotting
- •Logarithmic plots
- •More advanced graphical output
- •Exercises
- •Conditionals and Loops
- •Conditionals
- •Loops
- •List Comprehensions
- •Exercises
- •Functions
- •Methods and attributes
- •Exercises
- •Curve Fitting
- •Exercises
- •Numerical Routines: SciPy and NumPy
- •Special functions
- •Linear algebra
- •Solving non-linear equations
- •Solving ODEs
- •Discrete (fast) Fourier transforms
- •Exercises
- •Installing Python
- •IPython Notebooks
- •Python Resources
Introduction to Python for Science, Release 0.9.23
it works to use it. Just follow the examples shown below, which illustrate several different output formats using the print function with NumPy arrays.
In [16]: set_printoptions(
...: formatter={’float’: lambda x: format(x, ’6.2e’)})
In [17]: print(a)
[3.00e+00 5.67e+00 8.33e+00 1.10e+01 1.37e+01 1.63e+01 1.90e+01]
To specify the format of the output, you use the formatter keyword argument. The first entry to the right of the curly bracket is a string that can be ’float’, as it is above, or ’int’, or ’str’, or a number of other data types that you can look up in the online NumPy documentation. The only other thing you should change is the format specifier string. In the above example, it is ’6.2e’, specifying that Python should allocate at least 6 spaces, with 2 digits to the right of the decimal point in scientific (exponential) notation. For fixed width floats with 3 digits to the right of the decimal point, use the f in place of the e format specifier, as follows
In [18]: |
set_printoptions( |
|
|
|
|||
|
...: |
formatter={’float’: lambda |
x: format(x, ’6.3f’)}) |
||||
In [19]: |
print(a) |
|
|
|
|
||
[ |
3.000 |
5.667 |
8.333 |
11.000 |
13.667 |
16.333 |
19.000] |
To return to the default format, type the following
In [20]: set_printoptions(precision=8)
In [21]: print(a)
[3. 5.66666667 8.33333333 11.
13.66666667 16.33333333 19. |
] |
The set_printoptions is a NumPy function, so if you use it in a script or program, you should call it by writing np.set_printoptions.
4.3 File input
4.3.1 Reading data from a text file
Often you would like to analyze data that you have stored in a text file. Consider, for example, the data file below for an experiment measuring the free fall of a mass.
Data for falling mass experiment
Date: 16-Aug-2013
Data taken by Lauren and John
62 |
Chapter 4. Input and Output |
Introduction to Python for Science, Release 0.9.23
data point |
time (sec) |
height (mm) |
uncertainty (mm) |
0 |
0.0 |
180 |
3.5 |
1 |
0.5 |
182 |
4.5 |
2 |
1.0 |
178 |
4.0 |
3 |
1.5 |
165 |
5.5 |
4 |
2.0 |
160 |
2.5 |
5 |
2.5 |
148 |
3.0 |
6 |
3.0 |
136 |
2.5 |
7 |
3.5 |
120 |
3.0 |
8 |
4.0 |
99 |
4.0 |
9 |
4.5 |
83 |
2.5 |
10 |
5.0 |
55 |
3.6 |
11 |
5.5 |
35 |
1.75 |
12 |
6.0 |
5 |
0.75 |
We would like to read these data into a Python program, associating the data in each column with an appropriately named array. While there are a multitude of ways to do this in Python, the simplest by far is to use the NumPy loadtxt function, whose use we illustrate here. Suppose that the name of the text file is MyData.txt. Then we can read the data into four different arrays with the following statement
In [1]: dataPt, time, height, error = np.loadtxt("MyData.txt", skiprows=5 , unpack=True)
In this case, the loadtxt function takes three arguments: the first is a string that is the name of the file to be read, the second tells loadtxt to skip the first 5 lines at the top of file, sometimes called the header, and the third tells loadtxt to output the data (unpack the data) so that it can be directly read into arrays. loadtxt reads however many columns of data are present in the text file to the array names listed to the left of the “=” sign. The names labeling the columns in the text file are not used, but you are free to choose the same or similar names, of course, as long as they are legal array names. By the way, for the above loadtxt call to work, the file MyData.txt should be in the current working directory of the IPython shell. Otherwise, you need to specify the directory path with the file name.
It is critically important that the data file be a text file. It cannot be a MSWord file, for example, or an Excel file, or anything other than a plain text file. Such files can be created by a text editor programs like Notepad and Notepad++ (for a PC) or TextEdit and TextWrangler (for a Mac). They can also be created by MSWord and Excel provided you explicitly save the files as text files. Beware: You should exit any text file you make and save it with a program that allows you to save the text file using UNIX-type formatting, which uses a line feed (LF) to end a line. Some programs, like MSWord under Windows, may include a carriage return (CR) character, which can confuse loadtxt. Note that
4.3. File input |
63 |
Introduction to Python for Science, Release 0.9.23
we give the file name a .txt extension, which indicates to most operating systems that this is a text file, as opposed to an Excel file, for example, which might have a .xlsx or
.xls extension.
If you don’t want to read in all the columns of data, you can specify which columns to read in using the usecols key word. For example, the call
In [2]: time, height = loadtxt(’MyData.txt’, skiprows=5 , usecols = (1,2), unpack=True)
reads in only columns 1 and 2; columns 0 and 3 are skipped. As a consequence, only two array names are included to the left of the “=” sign, corresponding to the two column that are read. Writing usecols = (0,2,3) would skip column 1 and read in only the data in colums 0, 2, and 3. In this case, 3 array names would need to be provided on the left hand side of the “=” sign.
One convenient feature of the loadtxt function is that it recognizes any white space as a column separator: spaces, tabs, etc.
Finally you should remember that loadtxt is a NumPy function. So if you are using it in a Python module, you must be sure to include an “import numpy as np” statement before calling “np.loadtxt”.
4.3.2 Reading data from a CSV file
Sometimes you have data stored in a spreadsheet program like Excel that you would like to read into a Python program. The Excel data sheet shown below contains the same data set we saw above in a text file. While there are a number of different approaches one can use to reading such files, one of the simplest of most robust is to save the spreadsheet as a CSV (“comma separated value”) file, a format which all common spreadsheet programs can create and read. So, if your Excel spreadsheet was called MyData.xlsx, the CSV file saved using Excel’s Save As command would by default be MyData.csv. It would look like this
Data for falling mass experiment,,, Date: 16-Aug-2013,,,
Data taken by Lauren and John,,,
,,,
data point,time (sec0,height (mm),uncertainty (mm) 0,0,180,3.5 1,0.5,182,4.5 2,1,178,4 3,1.5,165,5.5 4,2,160,2.5
64 |
Chapter 4. Input and Output |
Introduction to Python for Science, Release 0.9.23
Figure 4.1: Excel data sheet
4.3. File input |
65 |