5

I am a newbie trying to learn data visuallizaion using python. Actually, I was just trying to follow the example given by a cookbook, like:

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"

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? https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html

1
  • 1
    try np.str or just str : data = numpy.loadtxt ('ch02-data.csv', dtype= numpy.str, delimiter=',') Commented Apr 27, 2017 at 8:14

2 Answers 2

13

Try dtype='str' instead of dtype='string'.

Sign up to request clarification or add additional context in comments.

Comments

7

Actually, it works well in Python2, but it doesn't work in Python 3.x, you can try numpy.str

In Python 2, there's no problem:

>>> 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:

>>> 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.

2 Comments

Curious that there has been no action on this bug report (not even a comment by developers) in two years.
I'm here August/2018 with the same problem. Apparently there is no solution from python, yet. Using here Python3.6.5 Anaconda version.

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.