【问题标题】:Get decimal data from Postgrades database using Django使用 Django 从 Postgrades 数据库中获取十进制数据
【发布时间】:2021-11-24 18:58:10
【问题描述】:

我使用 Python 和 Django 框架从 PostGress 数据库中获取一些十进制数据。 我的问题是变量“new_poi_lat”显示正确,但是为什么变量“poi_lat”显示如下?! 我希望“poi_lat”变量正常显示。 然后我想使用“zip(poi_lat,poi_log)”但它给出了一个错误!

我的代码:

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')]

【问题讨论】:

    标签: python django postgresql decimal


    【解决方案1】:

    这是因为有Decimal 对象,当你打印一个列表时,它会调用repr(…),而不是str(…)

    您可以轻松地将这些转换为floats,然后将它们放入一个列表中:

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

    【讨论】:

    • 谢谢。但我还是有问题。我不能使用 zip()。对于代码“coordinates = zip (poi_lat, poi_log)”,会出现此错误:TypeError: Object of type zip is not JSON serializable
    • @user16529463:如果你想 JSON 序列化它,你需要把它做成一个列表,所以coordinates = list(zip(poi_lat, poi_log))
    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2020-02-08
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多