Download (right-click, save target as ...) this page as a jupyterlab notebook from: ES-7
LAST NAME, FIRST NAME
R00000000
ENGR 1330 ES-7 - Homework
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
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
# 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)
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)
Read the contents of long_file.txt line-by-line and print each line as it is read.
# open the connection
# for ... # read line-by-line
# print() # each line
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.
# 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
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
# 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
# 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
List processing tips https://www.programiz.com/python-programming/del
Character replacement tips https://www.geeksforgeeks.org/python-string-replace/
Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm