# Function to read data from textbook files¶

In [1]:
# Written by Nishant Malik for course Math 50 Fall 2015 (Dartmouth College)

import urllib2
import numpy as np
str1='https://netfiles.umn.edu/users/nacht001/www/nachtsheim/Kutner/Chapter%20%20'
# chapter number
if(ch<10):
ch_str=str(ch).zfill(2)
else:
ch_str=str(ch)
if(pr<10):
pr_str=str(pr).zfill(2)
else:
pr_str=str(pr)

str2=str(ch)+'%20Data%20Sets/CH'+ch_str+'PR'+pr_str+'.txt'
str3=str1+str2
try: urllib2.urlopen(str3)
except urllib2.URLError as e:
print(e.reason), '\nChapter number or problem number is wrong. Try again!'
return np.array([])
data=[]
lines=line.strip('\r\n'); lines=lines.split();
data.append(lines)
colnos=len(data[0])
j=0; xdata = np.zeros((len(data),colnos))
for line in data:
for k in range(0,colnos):
xdata[j,k]=data[j][k]
j=j+1
return xdata



This function can also be saved in a separte file in your working directory, with a file name. For example lets save it as read_data_fx. It can be imported as shown below.

In [2]:
# import the above described function if saved in file named  "read_data_fx"


A wrong chapter number or problem number will raise a error (exception). For example:

In [3]:
#There is no data for problem 10 in chapeter 2

Not Found
Chapter number or problem number is wrong. Try again!

In [4]:
# There is data for problem 20 in chapter 1

In [5]:
# we can see what is inside data as follows
#no. of rows and columns in the data
rown,coln=data.shape
print 'numbers of columns =',coln, 'numbers of rows=', rown
print 'First column of the data =',  data[:,0]
print 'Second column of the data =',  data[:,1]

numbers of columns = 2 numbers of rows= 45
First column of the data = [  20.   60.   46.   41.   12.  137.   68.   89.    4.   32.  144.  156.
93.   36.   72.  100.  105.  131.  127.   57.   66.  101.  109.   74.
134.  112.   18.   73.  111.   96.  123.   90.   20.   28.    3.   57.
86.  132.  112.   27.  131.   34.   27.   61.   77.]
Second column of the data = [  2.   4.   3.   2.   1.  10.   5.   5.   1.   2.   9.  10.   6.   3.   4.
8.   7.   8.  10.   4.   5.   7.   7.   5.   9.   7.   2.   5.   7.   6.
8.   5.   2.   2.   1.   4.   5.   9.   7.   1.   9.   2.   2.   4.   5.]


## Function to read tables from examples in the textbook¶

In [6]:
def read_tb_dataTA(ch,pr):
import urllib2
import numpy as np
str1='https://netfiles.umn.edu/users/nacht001/www/nachtsheim/Kutner/Chapter%20%20'
# chapter number
if(ch<10):
ch_str=str(ch).zfill(2)
else:
ch_str=str(ch)
if(pr<10):
pr_str=str(pr).zfill(2)
else:
pr_str=str(pr)

str2=str(ch)+'%20Data%20Sets/CH'+ch_str+'TA'+pr_str+'.txt'
str3=str1+str2
try: urllib2.urlopen(str3)
except urllib2.URLError as e:
print(e.reason), '\nChapter number or example number is wrong. Try again!'
return np.array([])
data=[]
lines=line.strip('\r\n'); lines=lines.split();
data.append(lines)
colnos=len(data[0])
j=0; xdata = np.zeros((len(data),colnos))
for line in data:
for k in range(0,colnos):
xdata[j,k]=data[j][k]
j=j+1
return xdata


Again one can save this function in the same file read_data_fx and can import it as follows:

In [7]:
from read_data_fx import read_tb_dataTA

In [8]:
print data

[[ 125.  160.]
[ 100.  112.]
[ 200.  124.]
[  75.   28.]
[ 150.  152.]
[ 175.  156.]
[  75.   42.]
[ 175.  124.]
[ 125.  150.]
[ 200.  104.]
[ 100.  136.]]