AWS将SelectFields和Filter粘附在一起,而不采用动态值

2024-10-05 15:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个AWS Glue脚本,它使用SelectFields()Filter()方法进行字段选择和过滤。我已经用静态值测试了它们,效果很好,但是当动态值以相同的格式传递时,它们就不起作用了。知道为什么不取动态值吗? 我通过传递一个动态值进行了测试,对于本例,两种方法都有效。你知道吗

请注意,如果传递的密钥(filterkey)是静态的或动态的,那么它也可以工作

wordstoFilter = ['USA', 'France']
columnstoSelect = ['cust_id', 'custname', 'state']


#join and return all list values in single quote along with comma
fltr_string =', '.join(["'{}'".format(value) for value in wordstoFilter])
select_string =', '.join(["'{}'".format(value) for value in columnstoSelect ])


filterkey = "country"
#below statement works with static value
#country_filter_dyf = Filter.apply(frame=custData, f=(lambda x: x["country"] in ["USA"]))
country_filter_dyf = Filter.apply(frame=custData, f=(lambda x: x[filterkey] in [fltr_string]))

##Select case
#below statement works with static value
#selected_fields_dyf = SelectFields.apply(frame = custData, paths = ['cust_id', 'cust_name', 'state', 'country'])

#Below one doesn't work
selected_dyf = SelectFields.apply(frame = custData, paths = [select_string ])

Tags: instringvaluewith动态filterframecountry
1条回答
网友
1楼 · 发布于 2024-10-05 15:28:24

如我所见,paths参数要求您提供一个列表,但您提供一个str对象:

>>> type(['cust_id', 'cust_name', 'state', 'country'])
<class 'list'>
>>> type(select_string)
<class 'str'>

你有没有试着直接列出名单?你知道吗

>>> type(columnstoSelect)
<class 'list'>

columnstoSelect = ['cust_id', 'custname', 'state']
selected_dyf = SelectFields.apply(frame = custData, paths = columnstoSelect )

相关问题 更多 >