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

FIELDNAMES throwing an error?

for some reason I'm getting an error when I try to run my code in the workspace. it's saying that there's a sytax error with the variable "FIELDNAMES" I can't seem to find what it's talking about.

import csv
import numpy

def open_with_csv(filename, d='\t'):
  data = []
  with open(filename, encoding='utf') as tsvin:
    tie_reader = csv.reder(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', '|S50'), ('material', '|S50'), ]

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

my_csv = load_data('data.csv')"""

1 Answer

Hi Charlesdi

I cannot see anything wrong with the FIELDNAMES variable, although there are a few other errors in you code

data_from+csv=open_with_csv('data.csv')

# should be

data_from_csv=open_with_csv('data.csv')

# you have not passed in the delimiter to the function
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

Gotcha, thanks! I've made the changes to the code. I've decided to just write the code in a jupyter notebook. This way I'll be able to reference it later and upload it to github as well. It's unfortunate that I'm missing out on things such as actually executing the script and navigating the file system, but I'll live.