#! /usr/bin/env python3 # def csv_reader_test ( ): #*****************************************************************************80 # ## csv_reader_test() tests csv.reader(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 October 2024 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'csv_reader_test():' ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' numpy version: ' + np.version.version ) print ( ' csv.reader() reads information from a CSV file.' ); basketball_reader ( ) brc_reader ( ) # # Terminate. # print ( '' ) print ( 'csv_reader_test():' ) print ( ' Normal end of execution.' ); return def basketball_reader ( ): #*****************************************************************************80 # ## basketball_reader() reads data from a CSV file about basketball players. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 October 2024 # # Author: # # John Burkardt # import csv filename = 'basketball.csv' print ( '' ) print ( ' Reading data from "' + filename + '"' ) csv_file = open ( filename ) csv_reader = csv.reader ( csv_file, delimiter = ',') # # row[0] = index # row[1] = name # row[2] = position # row[3] = height # row[4] = weight # row[5] = salary # row[6] = shoe sponsorship (yes/no) # row[7] = career status # row[8] = age # line_count = 0 for row in csv_reader: if line_count == 0: print ( '' ) print ( f'Column names are {", ".join(row)}' ) else: print ( '' ) print(f'{row[1]} plays {row[2]}, height {row[3]} cm, weight {row[4]} pounds.') print(f'{row[1]} earns ${row[5]}.') if ( row[6] == 'yes' ): print(f'{row[1]} has a shoe sponsorship.') else: print(f'{row[1]} does not have a shoe sponsorship.') print(f'{row[1]}\'s career status is {row[7]}.') print(f'{row[1]}\'s age is {row[8]}.') line_count = line_count + 1 csv_file.close ( ) print ( '' ) print ( 'Processed', line_count, 'lines.' ) return def brc_reader ( ): #*****************************************************************************80 # ## brc_reader() reads data from a CSV file for billion record challenge. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 October 2024 # # Author: # # John Burkardt # import csv filename = 'brc_data.csv' print ( '' ) print ( ' Reading data from "' + filename + '"' ) csv_file = open ( filename ) csv_reader = csv.reader ( csv_file, delimiter = ',' ) # # row[0] = city name # row[1] = mean temperature # line_count = 0 for row in csv_reader: # # I often allow a final empty line in a CSV file. # The CSV reader will choke if I don't break at that point. # if ( len ( row ) == 0 ): print ( '' ) print ( ' Last line of file is empty!' ) break # # Remove double quotes around city name.' # row[0] = row[0].replace ( '"', '' ) print ( ' ', line_count, ' ' + row[0] + ' has mean temperature', row[1] ) line_count = line_count + 1 csv_file.close ( ) print ( '' ) print ( 'Processed', line_count, 'lines.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) csv_reader_test ( ) timestamp ( )