Python 拆分字符串不起作用

Python Split String dont work

无论我怎么尝试,这个字符串都没有拆分。

def funktion():
    ser = serial.Serial('COM5',115200)
    b = str("Das ist ein Test")
    a = str(ser.readline().decode())
    b.split(' ')
    a.split('s')
    print (a)
    print (b)

字符串不可变,因此您必须 re-assign 那些:

b = b.split(' ')
a = a.split('s')
print(a)
print(b)

Immutable vs Mutable types SO question and in that 文章中查看更多信息。

split 函数不会更改字符串 in-place。它 returns 一个新字符串。你必须改为 tokens = b.split(' '); print(b)