Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
我收到以下错误消息:

return decimal.Decimal(value)
ValueError: argument must be a sequence of length 3


并且从打印功能中打印出以下内容:

6
-a- 58.7178900
-b- 58.7178900
-c- (57.7087642,)


我的代码是:

divide_by = 10000000.0

def run():

    buildings = Building.objects.filter(pk__gte=6)

    lat_max = 577602387
    lat_min = 576345017
    lon_max = 121798975
    lon_min = 118813094

    lat_max2 = 593955175
    lat_min2 = 592513364
    lon_max2 = 181714751
    lon_min2 = 178795955

    for building in buildings:
        print(building.id)
        gc = building.visitingAddress.geoCoordinate
        if building.visitingAddress.city.id == 1:
            print('-a-', gc.latitude)
            a = get_range(lat_min, lat_max)
            print('-b-', gc.latitude)
            gc.latitude = a,
            print('-c-', gc.latitude)

            a = float(get_range(lon_min, lon_max))
            gc.longitude = a,

        elif building.visitingAddress.city.id == 2:
            a = get_range(lat_min2, lat_max2)
            gc.latitude = a,
            a = get_range(lon_min2, lon_max2)
            gc.longitude = a,
        gc.save()

def get_range(min, max):
    a = random.randrange(min, max, 1)/divide_by
    while (len(str(a)) > 10):
        a = random.randrange(min, max, 1)/divide_by

    return a


以及完整的堆栈跟踪:

6
-a- 58.7178900
-b- 58.7178900
-c- (57.7087642,)
Exception while running run() in 'crb.scripts.addpatch'
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Dev\Python34\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Dev\Python34\lib\site-packages\django\core\management\__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Dev\Python34\lib\site-packages\django_extensions\management\email_notifications.py", line 63, in run_from_argv
    super(EmailNotificationCommand, self).run_from_argv(argv)
  File "C:\Dev\Python34\lib\site-packages\django\core\management\base.py", line390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Dev\Python34\lib\site-packages\django_extensions\management\email_notifications.py", line 75, in execute
    super(EmailNotificationCommand, self).execute(*args, **options)
  File "C:\Dev\Python34\lib\site-packages\django\core\management\base.py", line441, in execute
    output = self.handle(*args, **options)
  File "C:\Dev\Python34\lib\site-packages\django_extensions\management\utils.py", line 71, in inner
    ret = func(self, *args, **kwargs)
  File "C:\Dev\Python34\lib\site-packages\django_extensions\management\commands\runscript.py", line 172, in handle
    run_script(mod, *script_args)
  File "C:\Dev\Python34\lib\site-packages\django_extensions\management\commands\runscript.py", line 80, in run_script
    mod.run(*script_args)
  File "C:\Django\cs\crb\scripts\addpatch.py", line 62, in run
    gc.save()
  File "C:\Dev\Python34\lib\site-packages\django\db\models\base.py", line 710, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\base.py", line 803, in _save_table
    forced_update)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\base.py", line 853, in _do_update
    return filtered._update(values) > 0
  File "C:\Dev\Python34\lib\site-packages\django\db\models\query.py", line 580,in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1048, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 818, in execute_sql
    sql, params = self.as_sql()
  File "C:\Dev\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1016, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
  File "C:\Dev\Python34\lib\site-packages\django\db\models\fields\__init__.py",line 1626, in get_db_prep_save
    return connection.ops.value_to_db_decimal(self.to_python(value),
  File "C:\Dev\Python34\lib\site-packages\django\db\models\fields\__init__.py",line 1597, in to_python
    return decimal.Decimal(value)
ValueError: argument must be a sequence of length 3

最佳答案

删除gc.longitude = a,末尾的逗号。逗号导致a,被解释为元组(a,)而不是浮点数。

关于python - 不再是浮点数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35101946/

10-12 18:54