SyntaxError:非ASCII字符'\ xe2'

时间:2014-08-07 19:28:43

标签: python python-2.7

我写了一个程序给我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)

但是当我去跑步时,我得到了

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

我需要做些什么来解决它?当我在我的pi上运行它时,它工作,但我似乎无法让它在我的笔记本电脑上运行。我的笔记本电脑正在运行Python 2.7.5

3 个答案:

答案 0 :(得分:14)

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

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

details

答案 1 :(得分:9)

您应该使用标准ASCII引号:

index = rawHTML.find("CCFF00")

而不是:

index = rawHTML.find(“CCFF00”)

答案 2 :(得分:0)

我遇到了这个问题,结果发现我在浏览器中进行的复制/粘贴操作复制了看起来像纯破折号的字符,但是没有。也许有些简单的事情要注意。