ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 17.04.2021
Просмотров: 3751
Скачиваний: 3

Introduction to Python for Science, Release 0.9.23
(string1)
(string1
+
’ ’
+
string2)
(
’ 1. {} {}’
.
format(string1, string2))
(
’ 2. {0:s} {1:s}’
.
format(string1, string2))
(
’ 3. {0:s} {0:s} {1:s} - {0:s} {1:s}’
.
format(string1, string2))
(
’ 4. {0:10s}{1:5s}’
.
format(string1, string2))
(
’ ***’
)
(int1, int2)
(
’ 6. {0:d} {1:d}’
.
format(int1, int2))
(
’ 7. {0:8d} {1:10d}’
.
format(int1, int2))
(
’ ***’
)
(
’ 8. {0:0.3f}’
.
format(float1))
(
’ 9. {0:6.3f}’
.
format(float1))
(
’10. {0:8.3f}’
.
format(float1))
(
2
*
’11. {0:8.3f}’
.
format(float1))
(
’ ***’
)
(
’12. {0:0.3e}’
.
format(float2))
(
’13. {0:10.3e}’
.
format(float2))
(
’14. {0:10.3f}’
.
format(float2))
(
’ ***’
)
(
’15. 12345678901234567890’
)
(
’16. {0:s}--{1:8d},{2:10.3e}’
.
format(string2, int1, float2))
Here is the output:
***
How
How are you my friend?
1. How are you my friend?
2. How are you my friend?
3. How How are you my friend? - How are you my friend?
4. How
are you my friend?
***
(34, 942885)
6. 34 942885
7.
34
942885
***
8. -3.000
9. -3.000
10.
-3.000
11.
-3.00011.
-3.000
***
60
Chapter 4. Input and Output

Introduction to Python for Science, Release 0.9.23
12. 3.142e-14
13.
3.142e-14
14.
0.000
***
15. 12345678901234567890
16. are you my friend?--
34, 3.142e-14
Successive empty brackets
{}
like those that appear in the statement above
print(’
1.
{} {}’.format(string1, string2))
are numbered consecutively start-
ing at 0 and will print out whatever variables appear inside the
format()
method using
their default format.
Finally, note that the code starting on lines 14 and 16 each are split into two lines. We
have done this so that the lines fit on the page without running off the edge. Python allows
you to break lines up like this to improve readability.
4.2.2 Printing arrays
Formatting NumPy arrays for printing requires another approach. As an example, let’s
create an array and then format it in various ways. From the IPython terminal
In [10]:
a
=
linspace(
3
,
19
,
7
)
In [11]:
(a)
[
3.
5.66666667
8.33333333
11.
13.66666667
16.33333333
19.
]
Simply using the
function does print out the array, but perhaps not in the
format you desire.
To control the output format, you use the NumPy function
set_printoptions
. For example, suppose you want to see no more than two digits
to the right of the decimal point. Then you simply write
In [12]:
set_printoptions(precision
=
2
)
In [13]:
(a)
[
3.
5.67
8.33
11.
13.67
16.33
19.
]
If you want to change the number of digits to the right of the decimal point to 4, you set
the keyword argument
precision
to 4
In [14]:
set_printoptions(precision
=
4
)
In [15]:
(a)
[
3.
5.6667
8.3333
11.
13.6667
16.3333
19.
]
Suppose you want to use scientific notation. The method for doing it is somewhat arcane,
using something called a
lambda
function. For now, you don’t need to understand how
4.2. Screen output
61

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
function with NumPy arrays.
In [16]:
set_printoptions(
...: formatter={’float’: lambda x: format(x, ’6.2e’)})
In [17]:
(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]:
(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]:
(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
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