Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab7-TH
LAST NAME, FIRST NAME
R00000000
ENGR 1330 Laboratory 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)
atomickitty sensei /opt/jupyterhub/bin/python3 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
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
myfile = open('definition.txt','w')
myfile.write(definition) # write the object block to the file
myfile.close()
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)
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)
482
Read the contents of long_file.txt line-by-line and print each line as it is read.
# open the connection
mystring = []
myfile = open('long_file.txt','r')
# for ... # read line-by-line
for line in myfile:
mystring.append(str(line))
# print() # each line
print(str(line),end="")
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)
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.
myfile = open('pythonista.txt','r') # open the input connection
myoutput = open('python_newbie_and_the_guru.txt','w') # open an output connection
for line in myfile: # read line-by-line from input
tmp = str(line) # typecast to string, store in a string variable
tmp = tmp.replace("Pythonista","Python newbie") # replace pythonista with python newbie in line just read
tmp = tmp.replace("Python snake","Python guru") # replace Python snake with Python guru in line just read
print(tmp,end="")
myoutput.write(tmp) # write updated line to output
myfile.close()
myoutput.close()
# read line-by-line from input
A blue Python newbie, green behind the ears, went to Pythonia. She wanted to visit the famous wise green Python guru. She wanted to ask her about the white way to avoid the black. The bright path to program in a yellow, green, or blue style. The green Python turned red, when she addressed her. The Python newbie turned yellow in turn. After a long but not endless loop the wise Python uttered: "The rainbow!"
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
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
# create null list(s) to store data
xyzmatrix = []
list1 = []
list2 = []
# open file connection
xyzfile = open('ts-data.txt','r')
# read line 1
line1 = (xyzfile.readline().split(",")) # read just the first line
# parse the line to get values for strx and stry to send to the plotter
strx = str(line1[0])
stry = str(line1[1])
# for ... read remaining lines
for line in xyzfile: # now read rest of the file, line by line
xyzmatrix.append([float(n) for n in line.strip().split(",")])
# close the connection
xyzfile.close()
# process lines to build lists
for i in range(0,len(xyzmatrix)):
list1.append(xyzmatrix[i][0])
list2.append(xyzmatrix[i][1])
plot_title = 'My Plot'
plotAline(list1,list2,strx,stry,plot_title)
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