Python BigQuery Attribute Error : 'Row' Object has no Attribute
Python BigQuery Attribute Error : 'Row' Object has no Attribute
我有一个行对象 row.total_bytes_processed
,其中包含 return None 的行。如果它 returns None 我有逻辑将值默认为 0
for row in rows:
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0 # <- Error occurs here
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
然而,当我这样做时,我收到了这个错误:
row.total_bytes_processed = 0
AttributeError: 'Row' object has no attribute 'total_bytes_processed'
如何修复此错误?我不能将 None(Nonetype) 默认为 0 吗?
我已验证所有行都有 total_bytes_processed。
这是我的源代码:
from google.cloud import bigquery
from google.oauth2 import service_account
sql = """
SELECT
job_id,
creation_time,
user_email,
query,
total_bytes_processed
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE project_id ='nj-dev-blah'
AND creation_time BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 183 DAY)
AND CURRENT_TIMESTAMP()
ORDER BY creation_time DESC
LIMIT 100
"""
query_job = client.query(sql)# Make an API request.
results = query_job.result()
rows = list(results)
print("The query data:")
# print(rows)
for row in rows:
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
拥有等于 "None" 的属性与根本没有这样的属性是不同的,如果您使用:
if row.total_bytes_processed is not None:
Python 将尝试访问此对象的名为 "total_bytes_processed" 的属性,然后将其与 None 进行比较。您收到此错误是因为在这种情况下,该对象的属性 "total_bytes_processed" 不存在。
您可以使用方法 "hasattr",提供您要查找的对象和属性的名称作为参数,如果参数存在,该方法将 return 为真,为假否则:
if hasattr(row, "total_bytes_processed"):
请记住,即使属性存在且等于 "None","hasattr" 仍将 return 为真,因此您可以将其作为外部验证,然后在您之后知道该属性存在,验证它是否等于 "None" 并采取相应行动。它会是这样的:
for row in rows:
if hasattr(row, "total_bytes_processed"):
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
else:
#code for when total_bytes_processed does not exists
我有一个行对象 row.total_bytes_processed
,其中包含 return None 的行。如果它 returns None 我有逻辑将值默认为 0
for row in rows:
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0 # <- Error occurs here
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
然而,当我这样做时,我收到了这个错误:
row.total_bytes_processed = 0
AttributeError: 'Row' object has no attribute 'total_bytes_processed'
如何修复此错误?我不能将 None(Nonetype) 默认为 0 吗?
我已验证所有行都有 total_bytes_processed。
这是我的源代码:
from google.cloud import bigquery
from google.oauth2 import service_account
sql = """
SELECT
job_id,
creation_time,
user_email,
query,
total_bytes_processed
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE project_id ='nj-dev-blah'
AND creation_time BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 183 DAY)
AND CURRENT_TIMESTAMP()
ORDER BY creation_time DESC
LIMIT 100
"""
query_job = client.query(sql)# Make an API request.
results = query_job.result()
rows = list(results)
print("The query data:")
# print(rows)
for row in rows:
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
拥有等于 "None" 的属性与根本没有这样的属性是不同的,如果您使用:
if row.total_bytes_processed is not None:
Python 将尝试访问此对象的名为 "total_bytes_processed" 的属性,然后将其与 None 进行比较。您收到此错误是因为在这种情况下,该对象的属性 "total_bytes_processed" 不存在。
您可以使用方法 "hasattr",提供您要查找的对象和属性的名称作为参数,如果参数存在,该方法将 return 为真,为假否则:
if hasattr(row, "total_bytes_processed"):
请记住,即使属性存在且等于 "None","hasattr" 仍将 return 为真,因此您可以将其作为外部验证,然后在您之后知道该属性存在,验证它是否等于 "None" 并采取相应行动。它会是这样的:
for row in rows:
if hasattr(row, "total_bytes_processed"):
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed/1024 **4) *5
print( f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(
row.job_id, row.creation_time, row.query, row.total_bytes_processed,cost_dollars))
else:
row.total_bytes_processed = 0
cost_dollars = (int(row.total_bytes_processed) / 1024 ** 4) * 5
print(f"JOB_ID : {row.job_id} | Creation_Time : {row.creation_time} | Query: {row.query} | Total_Bytes_processed : {row.total_bytes_processed} | Estimated_Cost : ${cost_dollars}".format(row.job_id, row.creation_time, row.query, row.total_bytes_processed, cost_dollars))
else:
#code for when total_bytes_processed does not exists