无法确定SyntaxError的原因:标识符中的字符无效

时间:2017-04-26 21:46:28

标签: python python-3.x

我试图使用Brickman在LEGO EV3上使用Python编码。当我尝试运行我的代码时,我收到以下错误

>>robot@ev3dev:~$ python3 CoffeePi_Test/Main.py
File "CoffeePi_Test/Main.py", line 14
  tags=[‘coffeepi’]
                 ^
  SyntaxError: invalid character in identifier

这是我的代码。我不确定是什么引发了这个错误。

 #!/usr/bin/env python3
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import time
import vending

import ev3dev.ev3 as ev3
from ev3dev.auto import *


tags=[legovend]
words=[]
#Variables that contains the user credentials to access Twitter API
#Two versions, redudancy if the first fails...
access_token = # redacted
access_token_secret = # redacted
consumer_key = # redacted
consumer_secret = # redacted

access_token2 = # redacted
access_token_secret2 = # redacted
consumer_key2 = # redacted
consumer_secret2 = # redacted


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        global mA,mB,home,cs, lastVend
        try:
            tweet = json.loads(data)
            tw
        except:
            pass

        if tags:
            if all(tag in tweet['text'].lower() for tag in tags):
                print (tweet['user']['screen_name'], ' - ', tweet['text'])

                if int(tweet['timestamp_ms'])>lastVend:
                    ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED)
                    ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.RED)

                    vending.onTweet(mA, mB, cs, home)

                    time.sleep(2)
                    lastVend = int(round(time.time() * 1000))+1000
                    ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.GREEN)
                    ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.GREEN)
        return True

    def on_error(self, status):
        print( status)
        return False


if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    print( 'here')
    global mA,mB,home,cs, lastVend
    mA = ev3.MediumMotor('outA')
    mB = ev3.MediumMotor('outB')
    home = mA.position - 40
    cs=ColorSensor()
    lastVend = int(round(time.time() * 1000))

    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    auth2 = OAuthHandler(consumer_key2, consumer_secret2)
    auth2.set_access_token(access_token2, access_token_secret2)
    stream2 = Stream(auth2, l)

    try:

        Sound.speak('Ready to go').wait()
        print ('trying 1')
        stream.filter(track=tags)
        print('trying 2')
        stream2.filter(track=tags)
        Sound.speak('Could not connect').wait()
    except Exception as e:
        print( e)

1 个答案:

答案 0 :(得分:1)

在stackoverflow上通常不欢迎幽默,但是这个规则有例外,所以让这个答案是个例外。

但让我们回过头来回答实际问题:

  

Can't determine cause of SyntaxError: invalid character in identifier

     

I am not sure what is triggering this error.

我必须承认,我笑着写完这个完全正确的答案:):

  

SyntaxError的原因是标识符中的无效字符。

标识符中的无效字符是字符:

[']

U + 2019'e2 80 99正确的单一报价标记

这里是Python代码,您可以通过它自己找到关于"字符"的详细信息。这导致了麻烦:

#!/usr/bin/ python3
# -*- coding: <utf8> -*-
# tags=[’coffeepi’]
coffeepi = "’"
print(ord(coffeepi), hex(ord(coffeepi)), bin(ord(coffeepi))) 
什么打印:

  

8217, 0x2019 ,0b10000000011001

点击此处

http://www.utf8-chartable.de/unicode-utf8-table.pl 我从哪里获得有关角色的信息,这里 https://docs.python.org/3/reference/lexical_analysis.html#identifiers 有关标识符中允许哪些字符的更多详细信息,因为并非所有字符都显然不属于允许的字符范围。

顺便说一句:这个&#34;字符&#34;不是像ASCII字符那样的单字节字符。如何在UTF-8中编码此字符在Unicode表(e2 80 99)中指定。

AND ...在您在问题中提供的整个代码中,没有任何迹象表明您指定了导致错误消息的内容。

你如何解决这个问题?

  

只需用标准引号替换这些奇怪的引号&#34; &#34;

ADDENDUM:奇怪的引号应该是字符串周围的标准引号,其中有一个单词放入列表中。由于Python解释器遇到了这个奇怪的引号,它假设它们是变量名(标识符)的一部分而不是字符串规范的一部分。这就是混淆错误消息的原因,因为您从第一眼看不到代码为什么Python解释器认为它处理变量名称(标识符)而不是引用的文本部分应该转换为一个Python字符串。