Python Forum

Full Version: Explanation of the left side of this statement please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The statement below is from here
I understand that it is slicing and stripping the nmea string, but what is the meaning of the left hand side part:
lat, _, lon = line.strip().split(',')[2:5]
In particular with the use of underscore?
An Nmea string example would look like:
$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F
And I understand we are extracting the lat and long as:
lat = 3723.46587704
lon = 12202.26957864
So how does it assign lat/lon in one line
(I actually used it successfully, but would like to understand it a bit better)
regards
Russell
in slow motion:
line is str
line.strip() will remove any white-space in the begining or end of str
.split(',') will produce a list
[2:5] is slicing, so the right side is ['3723.46587704','N','12202.26957864']

what you have is unpacking the list (or any other iterable) into 3 variables. _ by convention means throw-away value (i.e. you are not interested in it)
TLTR: this is iterable unpacking. on the left side you have 3 variables and you unpack the iterable (assign each element as value to respective name). there is also extended iterable unpacking

also there is chained assignment, e.g. spam = eggs = 'foo'. both spam and eggs value is 'foo'
just an FYI (which you may already know):

GPGGA is a log file that contains time, position and fix related data of the GNSS receiver
it uses the NMEA 0183 combined electrical and data specification
read more here: https://tronico.fi/OH6NT/docs/NMEA0183.pdf

also see: https://docs.novatel.com/oem7/Content/Lo...onNMEALogs

GNSS is the European Global navigation satellite.
see: https://www.gsa.europa.eu/european-gnss/what-gnss

The snippet as buran explains will contain just the latitude and longitude coordinates.
Thanks guys for the great explanations
regards
Russell