#! /usr/bin/env python # def s_to_digits ( s, n ): #*****************************************************************************80 # ## S_TO_DIGITS extracts N digits from a string. # # Discussion: # # The string may include spaces, letters, and dashes, but only the # digits 0 through 9 will be extracted. # # Example: # # S => 34E94-70.6 # N => 5 # D <= (/ 3, 4, 9, 4, 7 /) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, the string. # # Input, integer N, the number of digits to extract. # # Output, integer DVEC(N), the extracted digits. # import numpy as np from ch_is_digit import ch_is_digit from ch_to_digit import ch_to_digit from sys import exit s_len = len ( s ) s_pos = 0 d_pos = 0 dvec = np.zeros ( n, dtype = np.int32 ) while ( d_pos < n ): if ( s_len <= s_pos ): print ( '' ) print ( 'S_TO_DIGITS - Fatal error!' ) print ( ' Could not read enough data from string.' ) error ( 'S_TO_DIGITS - Fatal error!' ); c = s[s_pos] s_pos = s_pos + 1 if ( ch_is_digit ( c ) ): d = ch_to_digit ( c ) dvec[d_pos] = d d_pos = d_pos + 1 return dvec def s_to_digits_test ( ): #*****************************************************************************80 # ## S_TO_DIGITS_TEST tests S_TO_DIGITS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # import platform from i4vec_print import i4vec_print print ( '' ) print ( 'S_TO_DIGITS_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' S_TO_DIGITS: string -> digit vector' ) s = '34E94-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 5 dvec = s_to_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 5 digits:' ) s = '34E94-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 7 dvec = s_to_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 7 digits:' ) # # Terminate. # print ( '' ) print ( 'S_TO_DIGITS_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) s_to_digits_test ( ) timestamp ( )