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

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

5

Here is the culprit:

delimiter= ‘;’,

You need straight quotes, not smart quotes:

delimiter= ';',
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • now i am getting this error File "CsvToJson.py", line 10, in 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? – Marco Dinatsoli Jul 29 '15 at 23:02
  • http://stackoverflow.com/questions/17315635/csv-new-line-character-seen-in-unquoted-field-error – TigerhawkT3 Jul 29 '15 at 23:04
  • 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 – Marco Dinatsoli Jul 29 '15 at 23:07
  • 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 – Marco Dinatsoli Jul 29 '15 at 23:08
  • 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. – TigerhawkT3 Jul 29 '15 at 23:11
  • what is that `dialect` ? please . i tried to re save it using microsoft office on my mac os and still the same problem – Marco Dinatsoli Jul 29 '15 at 23:14
  • As an argument to the `csv.reader()` call, as seen in the linked answer. – TigerhawkT3 Jul 29 '15 at 23:20