1

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?

1
  • Please accept the answer, if the question is answered ;-) Commented Jan 19, 2018 at 10:15

1 Answer 1

2

You can use tolist:

In [1]: [np.array(b'000914_0017_01_0017_P00_01.tifresize.jpg', dtype='|S40').tolist()]
Out[1]: ['000914_0017_01_0017_P00_01.tifresize.jpg']

UPDATE:

You can also extend / improve this to the following with using flatten:

In [2]: np.array([np.array(b'000914_0017_01_0017_P00_01.tifresize.jpg', dtype='|S40').tolist()]).flatten()
Out[2]:
array(['000914_0017_01_0017_P00_01.tifresize.jpg'],
      dtype='|S40')

This also works with loadtxt:

In [3]: np.array([np.loadtxt('split_file_name', dtype=bytes).tolist()]).flatten()
Out[3]:
array(['000914_0017_01_0017_P00_01.tifresize.jpg',
       '000925_0017_01_0006_P00_01.tifresize.jpg',
       '000919_0017_01_0012_P00_01.tifresize.jpg'],
      dtype='|S40')
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Nico Albers,thank you very much, your method works! I would further know how np.array works here? It seems that it converts list containing of multiple characters back again to a array, right?
yes, exactly. I use this to have the possibility to use the flatten method to not have nested lists there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.