Python 语法错误:非 ASCII 字符 '\xe2'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25190659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:51:31  来源:igfitidea点击:

SyntaxError: Non-ASCII character '\xe2'

pythonpython-2.7

提问by AceFuzion

I wrote a program to give me the value of the Mega Doge Coin

我写了一个程序来告诉我 Mega Doge Coin 的价值

import time
import urllib2
from datetime import datetime

def get_HTML():
    response = urllib.request.urlopen('http://www.dogepay.com')
    html = response.read()
    return html

def get_Value(rawHTML):
    index = rawHTML.find(“CCFF00”)
    while(rawHTML[index] != “$”):
            index = index + 1
    index = index + 1
    value = “”
    while(rawHTML[index].isdigit() or rawHTML[index] == ‘.'):
            value = value + rawHML[index]
            index = index + 1
    return float(value)

def get_DateTime():
    now = datetime.now()
    return '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, 
                                  now.minute, now.second)

def print_Output(DogeCoinValue, TimeDate):
    print timeDate + “ $“ + str(dogeCoinValue)

while(True):
    rawHTML = get_HTML()
    dogeCoinValue = get_Value(rawHTML)
    timeDate = get_DateTime()
    print_Output(dogeCoinValue, timeDate)
    time.sleep(5)

But when I go to run it I get

但是当我去运行它时,我得到

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

What do I need to do to fix it? It worked when I was running it on my pi but I can't seem to get it to run on my laptop. My laptop is running Python 2.7.5

我需要做什么来修复它?当我在我的 pi 上运行它时它有效,但我似乎无法让它在我的笔记本电脑上运行。我的笔记本电脑运行的是 Python 2.7.5

回答by mgilson

You should use standard ASCII quotes:

您应该使用标准的 ASCII 引号:

index = rawHTML.find("CCFF00")

rather than:

而不是:

index = rawHTML.find(“CCFF00”)

回答by philshem

In addition to not using non-ascii quotation marks, you should add to the top line of your code:

除了不使用非 ascii 引号外,您还应该添加到代码的第一行:

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

details

细节

回答by Christy

I was running into this and it turns out that my copy/paste from my browser copied what looked like a plain dash character but wasn't. Maybe something simple to look out for.

我遇到了这个问题,结果我从浏览器复制/粘贴复制了看起来像普通破折号但不是。也许一些简单的事情需要注意。