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


Exercise Set 7: FILE it for later ...

LAST NAME, FIRST NAME

R00000000

ENGR 1330 ES-7 - Homework

In [1]:
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty
sensei
/opt/jupyterhub/bin/python3
3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]
sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)

Reading and Writing Files


Exercise 0

Consider the object below named definition

In [2]:
definition = """
A computer file is a computer resource for recording data discretely in a
computer storage device. Just as words can be written 
to paper, so can information be written to a computer
file. Files can be edited and transferred through the 
internet on that particular computer system."""

Write this into a file with the name file_definition.txt. When done, open the file using an editor and screen capture the contents for your solution

In [16]:
# code block for your solution

Screen capture of file (put the filename into the object below, then elevate (remove the leading spaces and run the markdown cell to render the image)

![](filename.png)
In [ ]:
 

Exercise 1

The code cell below will generate a file

In [17]:
long_file = """V. ad Lesbiam

VIVAMUS mea Lesbia, atque amemus,
rumoresque senum severiorum
omnes unius aestimemus assis!
soles occidere et redire possunt:
nobis cum semel occidit breuis lux,
nox est perpetua una dormienda.
da mi basia mille, deinde centum,
dein mille altera, dein secunda centum,
deinde usque altera mille, deinde centum.
dein, cum milia multa fecerimus,
conturbabimus illa, ne sciamus,
aut ne quis malus inuidere possit,
cum tantum sciat esse basiorum.
(GAIUS VALERIUS CATULLUS)"""

open('long_file.txt','w').write(long_file)
Out[17]:
482

Read the contents of long_file.txt line-by-line and print each line as it is read.

In [18]:
# open the connection
# for ... # read line-by-line
#     print()  # each line

Exercise 2

Build a script to read http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/pythonista.txt (you can download a copy of the file) line-by-line. Then after each line is read, replace any occurance of "Pythonista" with "Python newbie" in each line, and replace any occurance of "Python snake" with "Python guru" in each line. Write the output to another file named "python_newbie_and_the_guru.txt" and show the contents of the new file in a text editor.

In [19]:
# open the input connection
# open an output connection
# read line-by-line from input
#    replace pythonista with python newbie in line just read
#    replace Python snake with Python guru in line just read
#    write updated line to output

Exercise 3

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.

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!
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
# create null list(s) to store data
# 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 [ ]: