0

I'm trying to read in a .csv file and get this error

ValueError: could not convert string to float: '220'

Here's my code so far:

import csv
import matplotlib
import os

environment = []

# Initialise data dir
dir = os.getcwd()
print(dir)
parent = os.path.dirname(dir)
parent = os.path.dirname(parent)
parent = os.path.dirname(parent)
basedir = os.path.dirname(parent)
print(basedir)
datadir = os.path.join(basedir, 'data')
print(datadir)
inputdatadir = os.path.join(datadir, 'input')
print(inputdatadir)

# Open file and read.
file = os.path.join(inputdatadir, 'snowslope1.csv')
f = open(file, newline='') 
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for row in reader: # A list of rows
    rowlist = []
    for value in row: # A list of value
        rowlist.append(value)
        #print(value)
    environment.append(rowlist)
f.close()

# Plot environment.
matplotlib.pyplot.imshow(environment)
matplotlib.pyplot.show()

Can anybody help with what's going wrong here? I'm unsure where the error is as they all appear to be just numbers like this: 220,221,222,223 in my file snowslope1.csv

TIA

kateaew
  • 3
  • 2

1 Answers1

0

This looks like a UTF-8 BOM.
Try opening the file with encoding="utf-8-sig"

Wups
  • 2,489
  • 1
  • 6
  • 17