Search code examples
pythoniterable-unpacking

list received is bigger than expected


I am working on nmea processing for gps trackers, where I am processing it as a list of values on this way

"""
information package exmaple
41719.285,A,1623.5136,S,07132.9184,W,017.8,203.5,040613,,,A*6B|1.6|2375|1010|0000,0000|02CC000A138E96D6|11|0029560C
"""
gprmc, hdop, altitude, state, ad, baseid, csq, journey = information.split('|')
ptime, gpsindicator, lttd, ns, lgtd, ew, speed, course, pdate, dd, checksum = gprmc.split(',')

Then, sometimes data packages are bigger, however are well formed, it is because some customers re-configure devices with extra data than they need and make my program crash, so I am looking for a way that my code doesn't crash in these cases.


Solution

  • use slices

    gprmc, hdop, altitude, state, ad, baseid, csq, journey = information.split('|')[:8]
    data = gprmc.split(',')
    ptime, gpsindicator, lttd, ns, lgtd, ew, speed, course, pdate, dd = data[:10] 
    checksum = data[-1] 
    

    in python 3.x You can use wildcard

    gprmc, hdop, altitude, state, ad, baseid, csq, journey, *_ = information.split('|')
    (ptime, gpsindicator, lttd, ns, lgtd, 
     ew, speed, course, pdate, dd, *_, checksum) = gprmc.split(',')