我正在尝试使用 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)
请您参考如下方法:
在 stackoverflow 上通常不欢迎幽默,但这条规则也有异常(exception),所以让这个答案成为这样的异常(exception)。
但让我们回到回答实际问题:
Can't determine cause of SyntaxError: invalid character in identifier
I am not sure what is triggering this error.
我不得不承认,我是笑着写下这个完全正确的答案 :) :
The cause of the SyntaxError is an invalid character in identifier.
标识符中的无效字符是字符:
[ ’ ]
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 有关标识符中允许使用哪些字符的更多详细信息,因为并非所有字符都是并且显然这个字符不属于允许字符的范围。
顺便说一下:这个“字符”不是像 ASCII 字符那样的单字节字符。在 Unicode 表 (e2 80 99) 中指定了此字符如何以 UTF-8 编码。
并且......在您在问题中提供的整个代码中,没有任何痕迹表明您指定的内容会导致错误消息。
如何解决这个问题?
Just replace these strange quotes with standard quotes " "
附录:奇怪的引号可能应该是一个字符串周围的标准引号,其中有一个词被放入列表中。当 Python 解释器遇到这个奇怪的引号时,它假定它们是变量名(标识符)的一部分而不是字符串规范的一部分。这就是令人困惑的错误消息的原因,因为乍一看你没有从代码中看到为什么 Python 解释器认为它处理变量名(标识符)而不是应该转换为 Python 的引用文本部分字符串。
