合并Python中用空格分割的字符串

sha*_*ait 3 python string merge split

我有一部分代码像这样:

for line in response.body.split("\n"):
    if line != "": 
        opg = int(line.split(" ")[2])
        opc = int(line.split(" ")[3])
        status = int(line.split(" ")[5])
        if command == 'IDENTIFY':
            if opg==opcodegroupr and opc==opcoder:
                if status=="0":
            IEEEAddrRemoteDev = line.split(" ")[6:14]
        ret['success'] = "IDENTIFY: The value is %s " % (IEEEAddrRemoteDev)
        self.write(tornado.escape.json_encode(ret))
        self.finish()
Run Code Online (Sandbox Code Playgroud)

变量'line'就像这样:

1363011361 2459546910990453036 157 0 17 0 209 61 0 0 0 0 0 0 0 0 0 0 0 0 0 201
Run Code Online (Sandbox Code Playgroud)

例如,我将从6到14中取字段并"合并"彼此以打印IEEEAddrRemoteDev,就像整个字符串一样.

这是

IEEEAddrRemoteDev = line.split(" ")[6:14] 
Run Code Online (Sandbox Code Playgroud)

正确的方法?如果我写

print IEEEAddrRemoteDev
Run Code Online (Sandbox Code Playgroud)

我什么都没得到.

这里有些不对劲...

Dav*_*son 6

你想用join:

ret['success'] = "IDENTIFY: The value is %s " % (" ".join(IEEEAddrRemoteDev))
Run Code Online (Sandbox Code Playgroud)

但是,更大的问题是你的status=="0"行永远不是真的(因为status是一个int),把它改成

if status == 0:
Run Code Online (Sandbox Code Playgroud)