Ale
Ale

Reputation: 86

python csv.dictreader not readin

I'm trying to read a csv file I've previously created with DictWriter, and it seems ok:

Long,Lat,Value
-4.46811978,36.71191819,21.836857418129924
-4.46829768,36.71214269,1.437288805738615
-4.46858762,36.71216232,1.4251383119025087
-4.46893456,36.71041284,1.4248025480555202
-4.46896851,36.71016468,0.00885256853534053

The problem is that I'm getting {'/': 'h'} when I try to read, so I get a KeyError: 'Value', because there is no Value key in that dictionary:

with open(path,'r') as file:
    csv_file = csv.DictReader(path)
    for line in csv_file:
        print line
        values.append(line["Value"])
        lat_long.append(line["Lat"],line["Long"])
    file.close()

I've printed the path to verify it is the right one, and it is, so I'm lost on how to solve this.

Upvotes: 0

Views: 672

Answers (1)

Hugo
Hugo

Reputation: 102

You should call csv.DictReader() with file and not path.

If you pass path to csv.DictReader(), csv.DictReader() will parse the string path itself and not the file located at path.

Here is a correct version of your code:

with open(path,'r') as file:
    csv_file = csv.DictReader(file)
    for line in csv_file:
        print line
        values.append(line["Value"])
        lat_long.append(line["Lat"],line["Long"])

Upvotes: 2

Related Questions