#! /usr/bin/env python3 # def python_text_files ( ): import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'python_text_files():' ) print ( ' Exercises for reading and writing files.' ) # # Write a text file for a single array. # print ( '' ) print ( ' Use np.savetxt() to write an array to a text file.' ) print ( '' ) filename = 'hilbert_matrix.txt' H = np.zeros ( [ 5, 5 ] ) for i in range ( 0, 5 ): for j in range ( 0, 5 ): H[i,j] = 1 / ( i + j + 1 ) np.savetxt ( filename, H ) print ( ' Created the text file "' + filename + '"' ) # # Use np.loadtxt() to read a table from a text file. # All the data items must be of the same type. # print ( '' ) print ( ' Use np.loadtxt() to read a table of data from a text file.' ) print ( '' ) filename = 'hilbert_matrix.txt' data = np.loadtxt ( filename ) m, n = data.shape print ( 'The table in "' + filename + '" has ' + str ( m ) + ' rows and ' + str ( n ) + ' columns.' ) print ( 'The maximum entries in each column:' ) print ( np.max ( data, axis = 0 ) ) # # Write a text file one line at a time. # print ( '' ) print ( ' Use open(), .write(), and .close() to create a text file.' ) print ( '' ) filename = 'mystery.txt' output = open ( 'mystery.txt', 'w' ) output.write ( 'Yesterday, upon the stair,\n' ) output.write ( 'I met a man who wasn\'t there.\n' ) output.write ( 'He wasn\'t there again today\n' ) output.write ( 'I wish, I wish he\'d go away.\n' ) output.close() print ( ' Wrote the text file "' + filename + '"' ) # # Read the text file. # print ( '' ) print ( ' Use open(), .write(), and .close() to read a text file.' ) print ( '' ) print ( ' Contents of "' + filename + '"' ) print ( '' ) input = open ( filename, 'r' ) for line in input: print ( line, end = '' ) input.close ( ) # # Append more data to the text file. # print ( '' ) print ( ' Use the append mode in open() to add lines to an existing file.' ) print ( '' ) output = open ( 'mystery.txt', 'a' ) output.write ( '\n' ) output.write ( 'When I came home last night at three\n' ) output.write ( 'The man was waiting there for me\n' ) output.write ( 'But when I looked around the hall\n' ) output.write ( 'I couldn\'t see him there at all!\n' ) output.close() print ( ' More lines were added to "' + filename + '"' ) # # Use .split() to read individual words from a line of a file. # print ( '' ) print ( ' Use .split() to read words from lines from text file.' ) print ( '' ) filename = 'patient.txt' output = open ( 'patient.txt', 'w' ) output.write ( 'Alice 60 120.3\n' ) output.write ( 'Bob 65 200.5\n' ) output.write ( 'Carol 70 160.1\n' ) output.close() print ( ' Wrote the text file "' + filename + '"' ) print ( '' ) bio = open ( filename, 'r' ) for line in bio: w = line.split() name = w[0] height = int ( w[1] ) weight = float ( w[2] ) bmi = ( weight / 2.204 ) * ( 39.370 / height )**2 print ( name, height, weight, bmi ) bio.close ( ) # # Texting while deriving. # print ( '' ) print ( ' Texting while deriving.' ) print ( '' ) filename = 'divisors.txt' output = open ( filename, 'w' ) for n in range ( 1, 21 ): for i in range ( 1, n + 1 ): if ( ( n % i ) == 0 ): output.write ( ' ' + str ( i ) ) output.write ( '\n' ) output.close ( ) print ( ' Wrote file "' + filename + '"' ) # # Read lists of varying length. # print ( '' ) print ( ' Read lists of varying length.' ) print ( '' ) divisors = [] filename = 'divisors.txt' output = open ( filename, 'r' ) for line in output: w = line.split() for i in range ( 0, len ( w ) ): w[i] = int ( w[i] ) divisors.append ( w ) output.close ( ) print ( divisors ) # # Write several data items of different length and type. # print ( '' ) print ( ' Write several data items of different length and type' ) print ( '' ) A = np.array ( [ \ [ 11.0, 12.0, 0.0, 0.0, 15.0 ], \ [ 21.0, 22.0, 0.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 33.0, 0.0, 35.0 ], \ [ 0.0, 0.0, 0.0, 44.0, 0.0 ], \ [ 51.0, 0.0, 53.0, 0.0, 55.0 ] ] ) b = np.array ( [ 110.0, 65.0, 274.0, 176.0, 485.0 ] ) x = np.array ( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) filename = 'slap_matrix.txt' n = 5 nz = 11 sym = 0 rhs_given = 1 x_given = 1 output = open ( filename, 'w' ) output.write ( str ( n ) + ' ' ) output.write ( str ( nz ) + ' ' ) output.write ( str ( sym ) + ' ' ) output.write ( str ( rhs_given ) + ' ' ) output.write ( str ( x_given ) + '\n' ) for i in range ( 0, n ): for j in range ( 0, n ): if ( A[i,j] != 0.0 ): output.write ( str ( i ) + ' ' + str ( j ) + ' ' + str ( A[i,j] ) + '\n' ) for i in range ( 0, n ): output.write ( str ( b[i] ) + '\n' ) for i in range ( 0, n ): output.write ( str ( x[i] ) + '\n' ) output.close() print ( ' Created file"' + filename + '"' ) # # Read several data items of different length and type. # print ( '' ) print ( ' Carefully read lines of varying length and type.' ) print ( '' ) filename = 'slap_matrix.txt' input = open ( filename, 'r' ) stage = 'read_size' for line in input: w = line.split() if ( stage == 'read_size' ): n = int ( w[0] ) nz = int ( w[1] ) sym = int ( w[2] ) rhs_given = int ( w[3] ) x_given = int ( w[4] ) stage = 'read_matrix' iz = 0 A = np.zeros ( [ n, n ] ) elif ( stage == 'read_matrix' ): i = int ( w[0] ) j = int ( w[1] ) aij = float ( w[2] ) A[i,j] = aij iz = iz + 1 if ( nz <= iz ): if ( rhs_given == 0 ): break else: stage = 'read_rhs' b = np.zeros ( n ) i = 0 elif ( stage == 'read_rhs' ): b[i] = float ( w[0] ) i = i + 1 if ( n <= i ): if ( x_given == 0 ): break else: stage = 'read_x' x = np.zeros ( n ) i = 0 elif ( stage == 'read_x' ): x[i] = float ( w[0] ) i = i + 1 if ( n <= i ): break input.close ( ) print ( ' Matrix A:' ) print ( A ) print ( ' RHS b:' ) print ( b ) print ( ' Solution x:' ) print ( x ) Ax = np.dot ( A, x ) print ( ' Verify: A*x = ' ) print ( Ax ) return if ( __name__ == "__main__" ): python_text_files ( )