def get_strings(str_arr): """Return a list of strings from a character array. Strip the whitespace from the right and recode it as utf-8. """ return [line.rstrip(' \0').encode('latin-1').decode('utf-8') for line in chars_to_strings(str_arr)]
def get_strings_no_decode(str_arr): """Return a list of strings from a character array. Strip the whitespace from the right and return it to the character set it was saved in. """ return [line.rstrip(' \0').encode('latin-1') for line in chars_to_strings(str_arr)]
def get_strings(str_arr): """Return a list of strings from a character array. Strip the whitespace from the right and return it to the character set it was saved in. """ return [line.rstrip(' \0').encode('latin-1') for line in chars_to_strings(str_arr)]
def test_chars_strings(): # chars as strings strings = ['learn ', 'python', 'fast ', 'here '] str_arr = np.array(strings, dtype='U6') # shape (4,) chars = [list(s) for s in strings] char_arr = np.array(chars, dtype='U1') # shape (4,6) assert_array_equal(chars_to_strings(char_arr), str_arr) ca2d = char_arr.reshape((2, 2, 6)) sa2d = str_arr.reshape((2, 2)) assert_array_equal(chars_to_strings(ca2d), sa2d) ca3d = char_arr.reshape((1, 2, 2, 6)) sa3d = str_arr.reshape((1, 2, 2)) assert_array_equal(chars_to_strings(ca3d), sa3d) # Fortran ordered arrays char_arrf = np.array(chars, dtype='U1', order='F') # shape (4,6) assert_array_equal(chars_to_strings(char_arrf), str_arr) # empty array arr = np.array([['']], dtype='U1') out_arr = np.array([''], dtype='U1') assert_array_equal(chars_to_strings(arr), out_arr)
def test_chars_strings(): # chars as strings strings = ['learn ', 'python', 'fast ', 'here '] str_arr = np.array(strings, dtype='U6') # shape (4,) chars = [list(s) for s in strings] char_arr = np.array(chars, dtype='U1') # shape (4,6) assert_array_equal(chars_to_strings(char_arr), str_arr) ca2d = char_arr.reshape((2,2,6)) sa2d = str_arr.reshape((2,2)) assert_array_equal(chars_to_strings(ca2d), sa2d) ca3d = char_arr.reshape((1,2,2,6)) sa3d = str_arr.reshape((1,2,2)) assert_array_equal(chars_to_strings(ca3d), sa3d) # Fortran ordered arrays char_arrf = np.array(chars, dtype='U1', order='F') # shape (4,6) assert_array_equal(chars_to_strings(char_arrf), str_arr) # empty array arr = np.array([['']], dtype='U1') out_arr = np.array([''], dtype='U1') assert_array_equal(chars_to_strings(arr), out_arr)
def test_chars_strings(): # chars as strings strings = ["learn ", "python", "fast ", "here "] str_arr = np.array(strings, dtype="U6") # shape (4,) chars = [list(s) for s in strings] char_arr = np.array(chars, dtype="U1") # shape (4,6) assert_array_equal(chars_to_strings(char_arr), str_arr) ca2d = char_arr.reshape((2, 2, 6)) sa2d = str_arr.reshape((2, 2)) assert_array_equal(chars_to_strings(ca2d), sa2d) ca3d = char_arr.reshape((1, 2, 2, 6)) sa3d = str_arr.reshape((1, 2, 2)) assert_array_equal(chars_to_strings(ca3d), sa3d) # Fortran ordered arrays char_arrf = np.array(chars, dtype="U1", order="F") # shape (4,6) assert_array_equal(chars_to_strings(char_arrf), str_arr) # empty array arr = np.array([[""]], dtype="U1") out_arr = np.array([""], dtype="U1") assert_array_equal(chars_to_strings(arr), out_arr)