Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Error in the Loading Raw Data video

This is the error I have:

python s2v1.py                                                                                                             
['', 'id', 'priceLabel', 'name', 'brandId', 'brandName', 'imageLink', 'desc', 'vendor', 'print', 'material']                                      
Traceback (most recent call last):                                                                                                                
  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/numpy/lib/_iotools.py", line 855, in easy_dtype                               
    ndtype = np.dtype(ndtype)                                                                                                                     
TypeError: data type not understood                                                                                                               

During handling of the above exception, another exception occurred:

Traceback (most recent call last):                                                                                                                
  File "s2v1.py", line 25, in <module>                                                                                                            
    my_csv= load_data('data.csv')                                                                                                                 
  File "s2v1.py", line 22, in load_data                                                                                                           
    my_csv = numpy.genfromtxt(filename, delimiter=d, skip_header=1, invalid_raise=False, names = 'FIELDNAMES', dtype=DATATYPES)                   
  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/numpy/lib/npyio.py", line 1433, in genfromtxt      
 dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names)                                                                                 
  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/numpy/lib/_iotools.py", line 864, in easy_dtype                               
    ndtype = np.dtype(dict(formats=ndtype, names=names))                                                                                          
TypeError: data type "myint" not understood                                                                    

This is my code:

import csv
import numpy

def open_with_csv(filename, d='\t'):
    data = []
    with open(filename, encoding='utf-8') as tsvin:
        tie_reader = csv.reader(tsvin, delimiter='\t')
        for line in tie_reader:
            data.append(line)
    return data

data_from_csv = open_with_csv('data.csv')
print(data_from_csv[0])


FIELDNAMES = ['', 'id', 'priceLabel', 'name', 'brandId', 'brandName', 'imageLink', 'desc', 'vendor', 'patterned', 'material']

DATATYPES = [('myint', 'i'), ('myid' 'i'), ('price', 'f8'), ('name', 'a200'), ('brandID', '<i8'), ('brandName', 'a200'), ('imageUrl', '|s500'), ('description', '|s900'), ('vendor', '|S100'), ('pattern', '|S500'), ('material', '|S50'), ]


def load_data(filename, d='\t'):
    my_csv = numpy.genfromtxt(filename, delimiter=d, skip_header=1, invalid_raise=False, names = 'FIELDNAMES', dtype=DATATYPES)
    return my_csv

my_csv= load_data('data.csv')

[MOD: added ```python markdown formatting -cf]

1 Answer

It should be names=FIELDNAMES not names = 'FIELDNAMES':

def load_data(filename, d='\t'):
    my_csv = numpy.genfromtxt(
        filename, delimiter=d, skip_header=1, invalid_raise=False,
        names=FIELDNAMES, dtype=DATATYPES)
    return my_csv