Home > Net >  Get decimal data from Postgrades database using Django
Get decimal data from Postgrades database using Django

Time:11-26

I use Python and the Django framework to get some decimal data from the PostGress database. My question is that the variable "new_poi_lat" is displayed correctly, but why is the variable "poi_lat" displayed as shown below ?! I want the "poi_lat" variable to be displayed normally. And then I want to use "zip (poi_lat, poi_log)" but it gives an error!

My code:

poi_lat = []
poi_log = []
for id in IDs:
    poi_name =  end_poi.values_list('lat','log').filter(id=id)
    new_poi_lat = poi_name[0][0]
    new_poi_log = poi_name[0][1]
    print("new_poi_lat:" , new_poi_lat)

    poi_lat.append(new_poi_lat)
    poi_log.append(new_poi_log)

   print("poi_lat:" , poi_lat)
coordinates = (zip(poi_lat, poi_log))

output:
new_poi_lat: 34.791553
new_poi_lat: 34.804567

poi_lat: [Decimal('34.791553'), Decimal('34.804567')]

CodePudding user response:

That's because there are Decimal objects and when you print a list, it will call repr(…) on it, and not str(…).

You can easily convert these to floats and then put these in a list with:

poi_lat = []
poi_log = []
for id in IDs:
    poi_name =  end_poi.values_list('lat','log').get(id=id)
    new_poi_lat = poi_name[0]
    new_poi_log = poi_name[1]
    print("new_poi_lat:" , new_poi_lat)

    poi_lat.append(float(new_poi_lat))
    poi_log.append(float(new_poi_log))

   print("poi_lat:" , poi_lat)
coordinates = (zip(poi_lat, poi_log))
  • Related