【发布时间】:2020-05-04 21:44:17
【问题描述】:
我有一个简单的 HTML 页面,要求用户输入并提交他们选择的动物的名称。然后我有一个 python 函数,可以将输入的动物通用名称转换为它的科学名称,以及它能够转换到的分类级别。
例如:熊猫 -> Ailuropoda melanoleuca,物种
下一步我需要帮助,即获取第一个 python 函数的输出,并在 Google 的 BigQuery 上对 pandas 数据框进行查询。下面是表格预览的截图。
下面是我的 main.py 中的 sn-p,它包含查询功能,以及底部的函数调用:
def make_query(taxon, level):
project_id = "sentinel-system"
data_frame = pandas_gbq.read_gbq(
"SELECT * FROM `animal_database.gbif_occurrence` WHERE species=%s LIMIT 10, (taxon)",
project_id=project_id,
index_col=level)
number_of_images = len(data_frame.index)
credentials = service_account.Credentials.from_service_account_file(
'Sentinel System-a6746634aad2.json')
pandas_gbq.context.credentials = credentials
pandas_gbq.context.project = 'sentinel-system'
if occurrences > 0:
print('We found %d images of the animal you searched for!' %(number_of_images))
else:
print('Sorry, we couldn''t find any images of the animal you searched for.')
return 0
taxonomy, level = (common_to_sci('Panda'))
name = taxonomy[-1, -1]
print(name)
print(level)
submission_check = (make_query(name, level))
这个函数的问题是双重的。首先,较小的问题是当前运行 main.py 显示错误
google.api_core.exceptions.BadRequest: 400 Syntax error: Illegal input character "%" at [1:63]
使用 python 元组参数是我从here 学到的解决方案,我不确定这个 SQL 查询应该是什么样子。
第二个更普遍的问题是,我知道 SQL 查询需要引用与我的 BigQuery 表中的列标题中使用的完全相同的字符串。但是如果分类级别不是“物种”而是“属/科/目”呢?对于 'level' != 'species' 的情况,有没有办法让 SQL 查询更通用?
【问题讨论】:
标签: python sql pandas google-bigquery