Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中检测字符串中的字符_Python_Python 2.7 - Fatal编程技术网

在python中检测字符串中的字符

在python中检测字符串中的字符,python,python-2.7,Python,Python 2.7,我通过串口获取字符串,我使用 readport(linker).startswith('STUFF') 如果只有一行,则此方法有效,但当出现多行时,仅当读取的第一行包含该单词时,即im收到的消息,即: 资料1 资料2 资料3 资料4 如果我使用readport(linker).startswith('STUFF4')它将返回false,但如果我使用STUFF1,它将工作 可能是不同的线条,或者我遗漏了什么 代码是这样的 while state == 2: print state

我通过串口获取字符串,我使用

readport(linker).startswith('STUFF')

如果只有一行,则此方法有效,但当出现多行时,仅当读取的第一行包含该单词时,即im收到的消息,即:

资料1

资料2

资料3

资料4

如果我使用
readport(linker).startswith('STUFF4')
它将返回false,但如果我使用STUFF1,它将工作

可能是不同的线条,或者我遗漏了什么

代码是这样的

while state == 2:

    print state

    time.sleep(0.1) 

    if readport(linker).startswith('STUFF4') == True:

        results.append(readport(linker).rstrip())
        state = 3
        print results

    else:
        print results
        line = readport(linker).rstrip()
        results.append(line)
        state = 2

提前谢谢。

那么,您要说的是,
链接器可以是多行的。你最终不知道它是多行还是多行?所以有时候链接器只是一行,而其他时候它是“X”行?就是说@idjaw有时可以是1,而其他XIs在这些消息中有一个分隔符可以用来拆分?你怎么知道什么是完整的“线”?即使只有一行,也应该有某行结尾?每一行都以\r\nOK结尾。然后,您所要做的就是迭代
linker.split('\r\n')
->
以获取linker.split('\r\n'):
。在该循环中,每一行
都将是您希望执行
启动检查的内容。