简体   繁体   English

“ Numpy” TypeError:不理解数据类型“字符串”

[英]“Numpy” TypeError: data type “string” not understood

I am a newbie trying to learn data visuallizaion using python. 我是一个新手,尝试使用python学习数据可视化。 Actually, I was just trying to follow the example given by a cookbook , like: 实际上,我只是尝试遵循Cookbook给出的示例,例如:

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

but somehow it did not work out: 但以某种方式无法解决:

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

this is the data I used: "ch02-data.csv" 这是我使用的数据: “ ch02-data.csv”

there were some similar issues posted , but I am not sure I understood what the answer tried to explain. 张贴了一些类似的问题,但我不确定我是否理解答案试图解释的内容。 Also, I checked the manual of numpy.loadtext() , still the answer does not seem to be obvious to me... any suggestion? 另外,我查看了numpy.loadtext()手册,答案对我来说似乎并不明显……有什么建议吗? https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html https://docs.scipy.org/doc/numpy/reference/generation/numpy.loadtxt.html

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

Actually, it works well in Python2, but it doesn't work in Python 3.x, you can try numpy.str 实际上,它在Python2中运行良好,但在Python 3.x中却无法运行,您可以尝试numpy.str

In Python 2, there's no problem: 在Python 2中,没有问题:

>>> import numpy as np
>>> np.__version__
'1.12.0'
>>> np.dtype('string')
dtype('S')
>>> np.dtype('str')
dtype('S')

In Python 3, this throw an exception: 在Python 3中,这引发异常:

>>> 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')

you can see more details from this issue . 您可以从本期中看到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM