我不知道如何在一堆代码中使用正则表达式,我也不知道如何正确地编码它。我试着查找教程,指南,所有的东西。但我还是不明白,所以请帮我修复我的愚蠢代码。
工作总结,我试图编写一个小程序,控制我的电脑键盘通过吡咯烷酮和帮助微位。例如,当按下按钮A时,微位通过uart和我的python在接收消息的末尾将数据发送到我的计算机,尝试匹配它并执行相应的命令。
这是微位的代码
from microbit import *
uart.init(baudrate=57600, bits=8, parity=None, stop=1, tx=None, rx=None)
while True:
    if button_a.is_pressed():
        uart.write("Up")
        display.show(Image.ARROW_NW)
    if button_b.is_pressed():
        uart.write("Down")
        display.show(Image.ARROW_S)
    else:
        display.show(Image.ASLEEP)这是我的python 的代码
import re
import serial
import keyboard
serialPort = serial.Serial(port = "COM5", baudrate=57600,
                           bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
serialString = ""
while(1):
    (serialPort.in_waiting > 0)
    serialString = serialPort.readline()
    wordsa = ("up")
    wordsb = ("down")
    if():
        words = [word.lower() for word in wordsa if re.match('^[a-zA-Z]+', word)]
        keyboard.press_and_release('up')问题是,我想要重新匹配通过serialString = serial.Port.readline()的uart接收到的字符串或数据,如果微位发送上来,我希望python匹配接收到的数据是向上还是向下,然后分别按上或下键。在运行我的这个糟糕的代码时,没有出现错误,而且它根本不起作用。我认为这是一个非常愚蠢的问题,但请帮助我。这个问题已经烧到我的脑子里了。
发布于 2020-01-27 11:29:32
谢谢DarryIG,Profile Link,Kudos。他救了我的命
如果有像我这样的初学者只想在一根绳子上做一个匹配。您不需要使用正则表达式。您可以使用运算符==来完成它,而=是赋值的。
例如:
Game = ['Dota2', 'LoL']; #Games that you play
Like = ['Dota2', 'LoL']; #Games that your friend likes
if Game == Like:
    print ('True')
else:
    print ('False')输出:True
注:我知道我的答案很糟糕,但我只想继续回答这个问题,以防像我这样的人需要这些信息。感谢您的理解。
https://stackoverflow.com/questions/59915449
复制相似问题