我想在python中附加一个字符串列表,如下所示:
messages = ["hello","hi"]
预期输出
messages = ["hello","hi","bye"]
她是一个我有错误的例子:
" str对象没有属性"追加" "
messages = ["really","hey"]
user = ["0.0.0.0"]
serv.bind(('0.0.0.0',12800))
msg, addr = serv.recvfrom(1024)
msg = msg.decode()
user.append(addr)
messages.append(msg)
完整代码:
chat = ['Bienvenu dans le chat de Dungeons !','Welcome on Dungeon\'s chat']
user = ["really","hey"]
pseudo = ["foo","this"]
isConnect = [0]
serv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
user_tab = 0
serv.bind(('0.0.0.0',12800))
while(1):
msg, addr = serv.recvfrom(1024)
msg = msg.decode()
if (msg.find("[pseudo]")!=-1):
firstPlace = msg.find("[pseudo]")
secondPlace = msg.find("[pswd]")
if firstPlace != -1 and secondPlace != -1:
pseu = msg[firstPlace+9:secondPlace]
pswd = msg[secondPlace+7:]
print("New client with infos ip ",addr, ",pseudo", pseu, "and password", pswd)
user.append(addr)
pseudo.append(pseu)
isConnect.append("1")
send_connect="connect"
serv.sendto(send_connect.encode(),addr)
答案 0 :(得分:1)
>>> l = ["hello","hi"]
>>> l.append("bye")
>>> l
['hello', 'hi', 'bye']
答案 1 :(得分:0)
我找到了解决方案,但我真的不明白。 这是我改变的:
pseu = str(msg[firstPlace+9:secondPlace])