关于python:” Numpy” TypeError:数据类型” string”不被理解

“Numpy” TypeError: data type “string” not understood

我是一个尝试使用python学习数据可视化的新手。
实际上,我只是在尝试遵循食谱中的示例,
像:

1
2
3
4
5
import numpy
import os
os.chdir("Home/Desktop/Temporal_folder")
data = numpy.loadtxt ('ch02-data.csv', dtype= 'string', delimiter=',')
print (data)

但是以某种方式无法解决:

1
2
3
4
5
6
Traceback (most recent call last):
  File"Home/PycharmProjects/Learning/Datavisuallization.py", line 5, in <module>
    data = numpy.loadtxt ('ch02-data.csv', dtype= 'string', delimiter=',')
  File"Home/anaconda/lib/python3.6/site-packages/numpy/lib/npyio.py", line 930, in loadtxt
    dtype = np.dtype(dtype)
TypeError: data type"string" not understood

这是我使用的数据:" ch02-data.csv"

有一些类似的问题,但我不确定我是否理解答案试图解释的内容。
另外,我检查了numpy.loadtext()的手册,答案似乎仍然不明显。
有什么建议吗?
https://docs.scipy.org/doc/numpy/reference/generation/numpy.loadtxt.html


尝试使用dtype ='str'而不是dtype ='string'。


实际上,它在Python2中运行良好,但在Python 3.x中不起作用,您可以尝试numpy.str

在Python 2中,没有问题:

1
2
3
4
5
6
7
>>> import numpy as np
>>> np.__version__
'1.12.0'
>>> np.dtype('string')
dtype('S')
>>> np.dtype('str')
dtype('S')

在Python 3中,这引发异常:

1
2
3
4
5
6
7
8
9
>>> import numpy as np
>>> np.__version__
'1.11.3'
>>> np.dtype('string')
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
TypeError: data type"string" not understood
>>> np.dtype('str')
dtype('<U')

您可以从此期中看到更多详细信息。