Download (right-click, save target as ...) this page as a jupyterlab notebook from: ES-7.1-TH


Exercise Set 7.1: FILE it for later ...

LAST NAME, FIRST NAME

R00000000

ENGR 1330 ES-7 - Homework


Reading and Writing Files - using requests

Exercise 1

The file http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/ts-data.txt contains two columns as shown below:

    Time,Speed
    0.0,0.0
    1.0,3.0
    2.0,7.0
    3.0,12.0
    4.0,20.0
    5.0,30.0
    6.0,45.6

The first column is time in seconds, the second is the speed of an object in meters/second.

First download the file using the requests module

import requests # Module to process http/https requests
remote_url="http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/ts-data.txt" # set the url
rget = requests.get(remote_url, allow_redirects=True) # get the remote resource, follow imbedded links
localfile = open('ts-data.txt','wb') # open connection to a local file same name as remote
localfile.write(rget.content) # extract from the remote the contents,insert into the local file same name
localfile.close() # close connection to the local file

Build a script that reads the data in the file, and then captures from the first row the two column labels (as strings) to be passed to the plotAline function below. Also put the numeric values into two lists as floats for passing to the plotAline function. Put your code after the plotAline definition

In [20]:
# use this code as-is!

import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)

def plotAline(list1,list2,strx,stry,strtitle): # plot list1 on x, list2 on y, xlabel, ylabel, title
    from matplotlib import pyplot as plt # import the plotting library from matplotlibplt.show()
    plt.plot( list1, list2, color ='green', marker ='o', linestyle ='solid') # create a line chart, years on x-axis, gdp on y-axis
    plt.title(strtitle)# add a title
    plt.ylabel(stry)# add a label to the x and y-axes
    plt.xlabel(strx)
    plt.show() # display the plot
    return #null return
In [21]:
# put your code here !
# download the file
# read the file 
#   open file connection
#   read line 1
#   parse the line to get values for strx and stry to send to the plotter
#   for ... read remaining lines
#   close the connection
# process lines to build lists
# list1 = ...
# list2 = ...
# strx = '...'
# stry = '...'
# plot_title = '...'
# plotAline(list1,list2,strx,stry,plot_title) #activate this line to make the plot
In [ ]: