简体   繁体   English

类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”

[英]TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

I having trouble passing a function as a parameter to another function.我无法将函数作为参数传递给另一个函数。 This is my code:这是我的代码:

ga.py: ga.py:

def display_pageviews(hostname):
    pageviews_results = get_pageviews_query(service, hostname).execute()
    if pageviews_results.get('rows', []):
        pv = pageviews_results.get('rows')
        return pv[0]
    else:
        return None


def get_pageviews_query(service, hostname):  
    return service.data().ga().get(
        ids=VIEW_ID,
        start_date='7daysAgo',
        end_date='today',
        metrics='ga:pageviews',
        sort='-ga:pageviews',
        filters='ga:hostname==%s' % hostname,)

models.py:模型.py:

class Stats(models.Model):
    user = models.OneToOneField('auth.User')
    views = models.IntegerField()
    visits = models.IntegerField()
    unique_visits = models.IntegerField()

updatestats.py:更新统计.py:

class Command(BaseCommand):

    def handle(self, *args, **options):
        users = User.objects.all()
        try:
            for user in users:
                hostname = '%s.%s' % (user.username, settings.NETWORK_DOMAIN)
                stats = Stats.objects.update_or_create(
                    user=user,
                    views=display_pageviews(hostname),
                    visits=display_visits(hostname),
                    unique_visits=display_unique_visits(hostname),)
        except FieldError:
            print ('There was a field error.')

When I run this: python manage.py updatestats I get the error:当我运行这个: python manage.py updatestats我得到错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”

I don't know what's causing this.我不知道是什么原因造成的。 I've tried converting it to a string, but I get the same error.我尝试将其转换为字符串,但出现相同的错误。 Any ideas?有什么想法吗?

Full traceback:完整追溯:

Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/myusername/project/Dev/project_files/project/main/management/commands/updatestats.py", line 23, in handle
    unique_visits=display_unique_visits(hostname),)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/query.py", line 480, in update_or_create
    obj = self.get(**lookup)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/query.py", line 378, in get
    clone = self.filter(*args, **kwargs)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1203, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1099, in build_lookup
    return final_lookup(lhs, rhs)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/lookups.py", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/lookups.py", line 57, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1860, in get_prep_lookup
    return super(IntegerField, self).get_prep_lookup(lookup_type, value)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 744, in get_prep_lookup
    return self.get_prep_value(value)
  File "/Users/myusername/project/Dev/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1854, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Edit:编辑:

Alright, I understand what the issue is.好的,我明白是什么问题了。 I used the shell to get the type of function output:我使用shell来获取函数输出的类型:

>>> type(display_pageviews('test.domain.com'))
<class 'list'>

I tried with this but it is still considered as a list:我试过这个,但它仍然被认为是一个列表:

pv = pageviews_results.get('rows')[0]
    return pv

What the error is telling, is that you can't convert an entire list into an integer.错误说明的是,您无法将整个列表转换为整数。 You could get an index from the list and convert that into an integer:您可以从列表中获取索引并将其转换为整数:

x = ["0", "1", "2"] 
y = int(x[0]) #accessing the zeroth element

If you're trying to convert a whole list into an integer, you are going to have to convert the list into a string first:如果您尝试将整个列表转换为整数,则必须先将列表转换为字符串:

x = ["0", "1", "2"]
y = ''.join(x) # converting list into string
z = int(y)

If your list elements are not strings, you'll have to convert them to strings before using str.join :如果您的列表元素不是字符串,则必须在使用str.join之前将它们转换为字符串:

x = [0, 1, 2]
y = ''.join(map(str, x))
z = int(y)

Also, as stated above, make sure that you're not returning a nested list.此外,如上所述,请确保您没有返回嵌套列表。

int() argument must be a string, a bytes-like object, or a number, not 'NoneType' int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”

According to the error, you can't convert strings type data into integers, So if a column contains null values as NaN then the type of NaN is float, so you can convert that into float.根据错误,您不能将字符串类型数据转换为整数,因此,如果一列包含空值作为 NaN,则 NaN 的类型为浮点数,因此您可以将其转换为浮点数。

check type of NaN -> type(np.NaN) ==> float检查 NaN 的类型 -> type(np.NaN) ==> float

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39; - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”? - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'? TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是“NoneType”。 但我写了 int() - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int() 尝试替换列表中的项目时收到“TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是‘列表’” - Receiving "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'" when attempting to replace item in a list 如何在python中将图像列表转换为数字列表? TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ Image” - How to convert list of images into list of numbers in python? TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image' int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - int() argument must be a string, a bytes-like object or a number, not 'tuple' int() 参数必须是字符串、类似字节的对象或数字,而不是 &#39;QueryDict&#39; - int() argument must be a string, a bytes-like object or a number, not 'QueryDict'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM