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


Laboratory 7: FILE it for later ...

LAST NAME, FIRST NAME

R00000000

ENGR 1330 Laboratory 7 - Homework


Reading and Writing Files


Exercise 0

Consider the object below named definition

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

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)


Exercise 1

The code cell below will generate a file

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


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.

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


References

  1. List processing tips https://www.programiz.com/python-programming/del

  2. Character replacement tips https://www.geeksforgeeks.org/python-string-replace/

  3. Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm