【发布时间】:2022-11-11 19:03:52
【问题描述】:
从分隔符中包含多个字符的文件加载时,使用 numpy.loadtxt 和 numpy==1.23.4 会引发 TypeError:
from io import StringIO
import numpy as np
csv_file = StringIO("""1, 2
3, 4
5, 6
""")
print(np.loadtxt(csv_file, delimiter=", "))
Traceback (most recent call last):
File "/home/hayesall/Teaching/questions/nploadtxt/testing.py", line 9, in <module>
print(np.loadtxt(csv_file, delimiter=", "))
File "/home/hayesall/miniconda3/envs/timeseries/lib/python3.10/site-packages/numpy/lib/npyio.py", line 1318, in loadtxt
arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter,
File "/home/hayesall/miniconda3/envs/timeseries/lib/python3.10/site-packages/numpy/lib/npyio.py", line 979, in _read
arr = _load_from_filelike(
TypeError: Text reading control character must be a single unicode character or None; but got: ', '
对于numpy==1.22.4,这似乎不会发生:
[[1. 2.]
[3. 4.]
[5. 6.]]
【问题讨论】: