简体   繁体   English

生成具有多个键的 DynamoDB put_item

[英]generate a DynamoDB put_item with multiple keys

I am followingthis tuto .我正在关注这个教程 I need to do the same with a csv that contains 120 columns.我需要对包含 120 列的 csv 执行相同的操作。 In the tuto, Keys for items are given manually.在教程中,项目的密钥是手动给出的。 Is there any way to do that automatically?有没有办法自动做到这一点? I already have a list with the csv columns that I can use.我已经有了一个包含可以使用的 csv 列的列表。 All of my columns are Strings.我所有的列都是字符串。 This is the block I want to automatize:这是我想要自动化的块:

response = dyndb.put_item(
                TableName='emplist',
                Item={
                'empid' : {'N':str(empid)},
                'name': {'S':name},
                'salary': {'N':str(salary)},
                'parttime': {'BOOL':False},
                }
            )

My column names list is:我的列名列表是:

columns = ['A', 'B', 'C', ...... ]

Unfortunately, as the types can differ, it is best do it manually for some columns.不幸的是,由于类型可能不同,最好对某些列进行手动操作。 Check which one is predominant, automate that and do manually the rest.检查哪一个是主要的,自动化并手动完成其余的工作。

Let's assume 'S' is found in ~90 columns.假设在大约 90 列中找到了“S”。

for index, row_item in enumerate(row):
    items_dict['column_' + i] = {'S': row_item}

# now manually replace the ones with a different type or that have additional parsing
item_dict['column_4'] = {'N': int(row[4])}
...

# if you want different key names do:
item_dict['name'] = item_dict['column_5']
del item_dict['column_5']

response = dyndb.put_item(
            TableName='emplist',
            Item=item_dict
        )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM