-1

I am trying to get the longtitude, latitude and altitude from a GPS module on my Raspberry Pi B+.

Currently running this code here:

## Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps

if __name__ == '__main__':
    gps = microstacknode.hardware.gps.l80gps.L80GPS()
    while True:
        try:
            data = gps.get_gpgga()
        except microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError:
            continue
        List = [list(data.values())[x] for x in [7, 9, 12]]
        string=str(List)
        string = string[1:-1]
        text_file = open("/home/pi/fyp/gps.txt","a")
        text_file.write(string + "\n")
        time.sleep(1)

This is the output of said code:

0.0, 0.0, '10.2'
0.0, 0.0, '3.2'
0.0, 0.0, '10.1'
0.0, 0.0, '3.1'
0.0, 0.0, '4.5'
0.0, 0.0, '20.1'
0.0, 0.0, '3583.1232'
0.0, 0.0, '102.01'
0.0, 0.0, '32.131'
0.0, 0.0, '421.32'
0.0, 0.0, '12391.11'
0.0, 0.0, '323.411'

Is it possible to remove the '' quotes in the last section of the output and leave just the number like in the first two sections of the output?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87

1 Answers1

0

Because you're converting a list to a string, actually you really shouldn't do that. I'd suggest use ', '.join() instead:

List = [list(data.values())[x] for x in [7, 9, 12]]
string = ', '.join(map(str, List))

Let's see what's wrong:

>>> l = ['123', 456]
>>> str(l)
"['123', 456]"

>>> print(str(l))
['123', 456]

>>> ', '.join(map(str, l))
'123, 456'

>>> print(', '.join(map(str, l)))
123, 456

Because Python use quotes to says that: This object is a string.

And when you convert an object which has a string object within it, the quotes will still there instead of removed automatically.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87