gpt4 book ai didi

python - 如何将 Python Dict 映射到 Big Query Schema

转载 作者:行者123 更新时间:2023-11-28 22:42:28 24 4
gpt4 key购买 nike

我有一个包含一些嵌套值的字典:

my_dict = {
"id": 1,
"name": "test",
"system": "x",
"date": "2015-07-27",
"profile": {
"location": "My City",
"preferences": [
{
"code": "5",
"description": "MyPreference",
}
]
},
"logins": [
"2015-07-27 07:01:03",
"2015-07-27 08:27:41"
]
}

并且,我有一个大查询表架构,如下所示:

schema = {
"fields": [
{'name':'id', 'type':'INTEGER', 'mode':'REQUIRED'},
{'name':'name', 'type':'STRING', 'mode':'REQUIRED'},
{'name':'date', 'type':'TIMESTAMP', 'mode':'REQUIRED'},
{'name':'profile', 'type':'RECORD', 'fields':[
{'name':'location', 'type':'STRING', 'mode':'NULLABLE'},
{'name':'preferences', 'type':'RECORD', 'mode':'REPEATED', 'fields':[
{'name':'code', 'type':'STRING', 'mode':'NULLABLE'},
{'name':'description', 'type':'STRING', 'mode':'NULLABLE'}
]},
]},
{'name':'logins', 'type':'TIMESTAMP', 'mode':'REPEATED'}
]
}

我想遍历所有原始的 my_dict 并根据模式的结构构建一个新的字典。换句话说,迭代模式并从原始 my_dict 中选取正确的值。

要像这样构建一个新的字典(请注意,不复制模式中不存在的字段“系统”):

new_dict = {
"id": 1,
"name": "test",
"date": "2015-07-27",
"profile": {
"location": "My City",
"preferences": [
{
"code": "5",
"description": "MyPreference",
}
]
},
"logins": [
"2015-07-27 07:01:03",
"2015-07-27 08:27:41"
]
}

如果没有嵌套字段迭代简单的 dict.items() 和复制值可能会更容易,但是我如何构建新的字典以递归方式访问原始字典?

最佳答案

我已经构建了一个递归函数来执行此操作。我不确定这是否是更好的方法,但有效:

def map_dict_to_bq_schema(source_dict, schema, dest_dict):
#iterate every field from current schema
for field in schema['fields']:
#only work in existant values
if field['name'] in source_dict:
#nested field
if field['type'].lower()=='record' and 'fields' in field:
#list
if 'mode' in field and field['mode'].lower()=='repeated':
dest_dict[field['name']] = []
for item in source_dict[field['name']]:
new_item = {}
map_dict_to_bq_schema( item, field, new_item )
dest_dict[field['name']].append(new_item)
#record
else:
dest_dict[field['name']] = {}
map_dict_to_bq_schema( source_dict[field['name']], field, dest_dict[field['name']] )
#list
elif 'mode' in field and field['mode'].lower()=='repeated':
dest_dict[field['name']] = []
for item in source_dict[field['name']]:
dest_dict[field['name']].append(item)
#plain field
else:
dest_dict[field['name']]=source_dict[field['name']]

format_value_bq(source_dict[field['name']], field['type'])

new_dict = {}
map_dict_to_bq_schema (my_dict, schema, new_dict)

关于python - 如何将 Python Dict 映射到 Big Query Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com