2

I am readying a cvs file,

this is my code:

import csv class CsvToJson: def __init__(self, csvFilePath): with open(csvFilePath, 'rb') as csvFile: spamreader = csv.reader(csvFile, delimiter= ‘;’, quotechar = '|') for row in spamreader: print ', '.join(row) k = CsvToJson(csvFilePath = 'carsModelsMakes.csv') 

I got this error

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

on the fifth line.

I read on internet and it seems the solution is to use

# -*- coding: utf-8 -*- 

in the beginning of the file.

I did that but then i got this error:

File "CsvToJson.py", line 6 spamreader = csv.reader(csvFile, delimiter= ‘;’, quotechar = '|') SyntaxError: invalid syntax 

could you help please

1 Answer 1

5

Here is the culprit:

delimiter= ‘;’, 

You need straight quotes, not smart quotes:

delimiter= ';', 
Sign up to request clarification or add additional context in comments.

7 Comments

now i am getting this error File "CsvToJson.py", line 10, in <module> k = CsvToJson(csvFilePath = 'carsModelsMakes.csv') File "CsvToJson.py", line 7, in init for row in spamreader: _csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
i tried to check the solution there, they say to change my rb to rU, but then i got a new error which is File "CsvToJson.py", line 5 with open(csvFilePath, 'r’) as csvFile: ^ SyntaxError: EOL while scanning string literal
SyntaxError: EOL while scanning string literal Williams-MacBook-Pro:ExcelToJson williamkinaan$ python CsvToJson.py File "CsvToJson.py", line 5 with open(csvFilePath, 'rU’) as csvFile: ^ SyntaxError: EOL while scanning string literal
You'll have to indicate the correct dialect that was used to create your CSV file. If it was created manually, without a specific dialect, you'll have to fix it manually, adhering to a specific dialect. A program like Excel may be able to help you there.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.