Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging a string splitted by space in Python

I have a part of code like this:

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()

Variable 'line' is like this for example:

1363011361 2459546910990453036 157 0 17 0 209 61 0 0 0 0 0 0 0 0 0 0 0 0 0 201

I would for example take fields from 6 to 14 and "merging" each others to print IEEEAddrRemoteDev like a entire string.

Is this

IEEEAddrRemoteDev = line.split(" ")[6:14] 

the correct way? If I write

print IEEEAddrRemoteDev

I don't obtain anything.

There's something wrong...

like image 516
sharkbait Avatar asked Nov 18 '25 10:11

sharkbait


1 Answers

You want to use join:

ret['success'] = "IDENTIFY: The value is %s " % (" ".join(IEEEAddrRemoteDev))

However, the bigger issue is that your status=="0" line is never true (because status is an int), change it to

if status == 0:
like image 179
David Robinson Avatar answered Nov 20 '25 02:11

David Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!