简体   繁体   English

添加#-*-编码后的python语法错误:utf-8-*-

[英]python syntax error after adding # -*- coding: utf-8 -*-

I am readying a cvs file, 我正在准备一个cvs文件,

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 你能帮忙吗

Here is the culprit: 罪魁祸首:

delimiter= ‘;’,

You need straight quotes, not smart quotes: 您需要直接引号,而不是智能引号:

delimiter= ';',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM