I read a file that has multiple lines to indicate filenames with np.loadtxt
for example, the txt file content is:
000914_0017_01_0017_P00_01.tifresize.jpg
000925_0017_01_0006_P00_01.tifresize.jpg
000919_0017_01_0012_P00_01.tifresize.jpg
This txt file has file name split_file_name.
I use for loops to decode each file name and do some processing for each image as:
for file_name in list(np.loadtxt(split_file_name, dtype=bytes)):
file_name.decode("utf-8")
# other processing...
The output for list(np.loadtxt(split_file_name, dtype=bytes)) is:
[b'000914_0017_01_0017_P00_01.tifresize.jpg',
b'000925_0017_01_0006_P00_01.tifresize.jpg',
b'000919_0017_01_0012_P00_01.tifresize.jpg']
however, when there is only one line in the file split_file_name, as:
000914_0017_01_0017_P00_01.tifresize.jpg
after using np.loadtxt() the output is:
array(b'000914_0017_01_0017_P00_01.tifresize.jpg', dtype='|S40')
when use list(np.loadtxt(split_file_name, dtype=bytes)) in this case,
it fails with
TypeError: iteration over a 0-d array.
The reason is that after np.loadtxt it returns an numpy array object which has only one long string, but list() cannot convert it directly to a list contain only one b'000914_0017_01_0017_P00_01.tifresize.jpg'
How should I do to ensure that it works for only one line txt file?