TypeError: expected str, bytes or os.PathLike object, not ndarray
时间: 2023-10-24 18:09:18 浏览: 286
解决Python中报错TypeError: must be str, not bytes问题
5星 · 资源好评率100%
This error occurs when you pass a numpy ndarray (N-dimensional array) as an argument to a function or method that expects a string, bytes, or os.PathLike object.
To fix this error, you need to convert the ndarray to a string, bytes, or os.PathLike object before passing it to the function or method. The specific conversion method depends on the function or method being used.
For example, if you are trying to open a file using the `open()` function and passing an ndarray as the filename, you can convert the ndarray to a string using the `str()` function:
```
import numpy as np
# create an ndarray
arr = np.array([1, 2, 3])
# open a file using the filename from the ndarray
with open(str(arr), 'w') as f:
f.write('Hello, world!')
```
In this example, the `str()` function is used to convert the ndarray `arr` to a string before passing it to the `open()` function.
阅读全文