#! /usr/bin/env python # def file_column_count ( filename ): #*****************************************************************************80 # ## FILE_COLUMN_COUNT counts the number of words in a typical column of a file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 October 2014 # # Author: # # John Burkardt # # Parameters: # # Input, string FILENAME, the name of the file. # # Output, integer COLUMN_COUNT, the number of words in a typical column. # column_count = -1 input = open ( filename, 'r' ) column_count = 0 for line in input: if ( line[0] == '#' ): continue else: wc = 0 for word in line.strip().split(): wc = wc + 1 if ( wc == 0 ): continue elif ( column_count == 0 ): column_count = wc break input.close ( ) return column_count def file_column_count_test ( ): #*****************************************************************************80 # ## FILE_COLUMN_COUNT_TEST tests FILE_COLUMN_COUNT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 03 December 2014 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'FILE_COLUMN_COUNT_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Count the number of columns in a typical text file line.' ) filename = 'r8mat_write_test.txt' column_count = file_column_count ( filename ) print ( '' ) print ( ' Number of columns in "%s" is %d' % ( filename, column_count ) ) # # Terminate. # print ( '' ) print ( 'FILE_COLUMN_COUNT_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) file_column_count_test ( ) timestamp ( )