Yograj Singh Mandloi
Yograj Singh Mandloi

Reputation: 603

Error while reading csv file in python

I tried to run following programme in ubuntu terminal but I am getting some error. But it is not giving any error in jupyter notebook

File "imsl.py", line 5 SyntaxError: Non-ASCII character '\xe2' in file imsl.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

import numpy
import matplotlib.pyplot


data_file ​= open("mnist_train_100.csv",'r') 
data_list ​=  ​data_file.readlines() 
data_file.close() 

Upvotes: 0

Views: 1630

Answers (3)

Dror Av.
Dror Av.

Reputation: 1214

You have a stray Unicode byte in the places where ? is in this code:

import numpy
import matplotlib.pyplot


data_file ?= open("mnist_train_100.csv",'r') 
data_list ?=  ?data_file.readlines() 
data_file.close() 

Correct it, and you're good to go.

Upvotes: 0

Milk
Milk

Reputation: 2655

You've got a stray byte floating around. You can find it by running

with open("imsl.py") as fp:
    for i, line in enumerate(fp):
        if "\xe2" in line:
            print i, repr(line)

You'll see the line number and the offending line(s). You can then delete the line and recreate it to remove the stray byte.

You could also add # -*- coding: utf-8 -*- to the top of the file to enforce encoding, as per your link.

Upvotes: 2

Ajax1234
Ajax1234

Reputation: 71471

Try using the built in csv library:

import csv

data_file = list(csv.reader(open('mnist_train_100.csv')))

Upvotes: 0

Related Questions