繁体   English   中英

如何将 Python Boto3 中的 DynamoDB 扫描格式化为 html 可读?

[英]How to format a DynamoDB scan in Python Boto3 to be human readable to html?

我有一个在 flask 中运行的 python boto3 应用程序。 主要目标是将列表检索格式化为人类可读的。 如:我希望列表返回为:

Employee Name        Employee ID
 Finn                  1001
 Jake                  1002
 Bubblegum             1003
 Marceline             1004
 BMO                   1005

等等。

目前,当我得到全表检索的响应时,它看起来像这样:

[{'EmployeeID': Decimal('1007'), 'Employee Name': 'Lumpy Space Princess'}, {'EmployeeID': Decimal('1021'), 'Employee Name': 'Glob'}, {'EmployeeID': Decimal('1029'), 'Employee Name': 'Snail'}, {'EmployeeID': Decimal('1038'), 'Employee Name': 'Banana Guard #216'}, {'EmployeeID': Decimal('1010'), 'Employee Name': 'Martin Mertens'}, {'EmployeeID': Decimal('1015'), 'Employee Name': 'Patience St. Pim'}, {'EmployeeID': Decimal('1035'), 'Employee Name': 'Cinnamon Bun'}, {'EmployeeID': Decimal('1002'), 'Employee Name': 'Jake'}, {'EmployeeID': Decimal('1018'), 'Employee Name': 'GOLB'}, {'EmployeeID': Decimal('1008'), 'Employee Name': 'Flame Princess'}, {'EmployeeID': Decimal('1032'), 'Employee Name': 'Jermaine'}, {'EmployeeID': Decimal('1014'), 'Employee Name': 'Earl of Lemongrab'}, {'EmployeeID': Decimal('1022'), 'Employee Name': 'Grod'}, {'EmployeeID': Decimal('1009'), 'Employee Name': 'Lady Rainicorn'}, {'EmployeeID': Decimal('1034'), 'Employee Name': 'Margaret'}, {'EmployeeID': Decimal('1023'), 'Employee Name': 'King of Mars'}, {'EmployeeID': Decimal('1001'), 'Employee Name': 'Finn'}, {'EmployeeID': Decimal('1025'), 'Employee Name': 'Tiny Manticore'}, {'EmployeeID': Decimal('1005'), 'Employee Name': 'Marceline The Vampire Queen'}, {'EmployeeID': Decimal('1005'), 'Employee Name': 'Marceline the Vampire Queen'}, {'EmployeeID': Decimal('1017'), 'Employee Name': 'Uncle Gumbald'}, {'EmployeeID': Decimal('1036'), 'Employee Name': 'Starchy'}, {'EmployeeID': Decimal('1026'), 'Employee Name': 'Magic Man'}, {'EmployeeID': Decimal('1037'), 'Employee Name': 'Banana Guard #1'}, {'EmployeeID': Decimal('1011'), 'Employee Name': 'Betty Grof'}, {'EmployeeID': Decimal('1012'), 'Employee Name': 'King of Ooo'}, {'EmployeeID': Decimal('1030'), 'Employee Name': 'Huntress Wizard'}, {'EmployeeID': Decimal('1006'), 'Employee Name': 'BMO'}, {'EmployeeID': Decimal('1031'), 'Employee Name': 'Forest Spirit'}, {'EmployeeID': Decimal('1016'), 'Employee Name': 'Fern'}, {'EmployeeID': Decimal('1033'), 'Employee Name': 'Joshua '}, {'EmployeeID': Decimal('1003'), 'Employee Name': 'Bonnibel Bubblegum'}, {'EmployeeID': Decimal('1024'), 'Employee Name': 'Death'}, {'EmployeeID': Decimal('1020'), 'Employee Name': 'Gob'}, {'EmployeeID': Decimal('1027'), 'Employee Name': 'Prismo'}, {'EmployeeID': Decimal('1019'), 'Employee Name': 'Grob'}, {'EmployeeID': Decimal('1028'), 'Employee Name': 'Gunter'}, {'EmployeeID': Decimal('1013'), 'Employee Name': 'Hunson Abadeer'}]

它只是一个元素。 它既无序,又具有所有元数据。 我所有的谷歌搜索,我都找不到如何提取实际数据字段以格式化为 HTML 人类可读兼容性的列表。

我的检索如下所示:

resource = boto3.resource('dynamodb',
aws_access_key_id="AAAAAAAAAAAAAAAAAAAA",
aws_secret_access_key="BBBBBBBBBBBBBBBBBBBBBB",
region_name='us-east-1')
    table = resource.Table('external-data')

    response = table.scan()
    data = response['Items']

while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
        data.append(response['Items'])

    ansExit = ''
    return str(data)

我尝试创建一个列表并将每个响应作为一个元素附加,但这个响应只是一个元素。 我想我需要把它分解成一个数组,这样我就可以将它打印到 HTML 作为 arr[i,j] where arr[1,1] = "1001", arr[1,2] = "Finn", arr [2,1] = “1002”,arr[2,2] = “杰克”,依此类推。 这是 go 的正确方向吗? 我什至不确定这是否可能,或者这只是不是 DynamoDB 数据的读取方式。

我只想隔离姓名和员工编号,这样我就可以在没有元数据的情况下打印它们。

谢谢你。

您可以使用pandas package:

import pandas as pd
from decimal import Decimal

data = [{'EmployeeID': Decimal('1007'), 'Employee Name': 'Lumpy Space Princess'}, {'EmployeeID': Decimal('1021'), 'Employee Name': 'Glob'}, {'EmployeeID': Decimal('1029'), 'Employee Name': 'Snail'}, {'EmployeeID': Decimal('1038'), 'Employee Name': 'Banana Guard #216'}, {'EmployeeID': Decimal('1010'), 'Employee Name': 'Martin Mertens'}, {'EmployeeID': Decimal('1015'), 'Employee Name': 'Patience St. Pim'}, {'EmployeeID': Decimal('1035'), 'Employee Name': 'Cinnamon Bun'}, {'EmployeeID': Decimal('1002'), 'Employee Name': 'Jake'}, {'EmployeeID': Decimal('1018'), 'Employee Name': 'GOLB'}, {'EmployeeID': Decimal('1008'), 'Employee Name': 'Flame Princess'}, {'EmployeeID': Decimal('1032'), 'Employee Name': 'Jermaine'}, {'EmployeeID': Decimal('1014'), 'Employee Name': 'Earl of Lemongrab'}, {'EmployeeID': Decimal('1022'), 'Employee Name': 'Grod'}, {'EmployeeID': Decimal('1009'), 'Employee Name': 'Lady Rainicorn'}, {'EmployeeID': Decimal('1034'), 'Employee Name': 'Margaret'}, {'EmployeeID': Decimal('1023'), 'Employee Name': 'King of Mars'}, {'EmployeeID': Decimal('1001'), 'Employee Name': 'Finn'}, {'EmployeeID': Decimal('1025'), 'Employee Name': 'Tiny Manticore'}, {'EmployeeID': Decimal('1005'), 'Employee Name': 'Marceline The Vampire Queen'}, {'EmployeeID': Decimal('1005'), 'Employee Name': 'Marceline the Vampire Queen'}, {'EmployeeID': Decimal('1017'), 'Employee Name': 'Uncle Gumbald'}, {'EmployeeID': Decimal('1036'), 'Employee Name': 'Starchy'}, {'EmployeeID': Decimal('1026'), 'Employee Name': 'Magic Man'}, {'EmployeeID': Decimal('1037'), 'Employee Name': 'Banana Guard #1'}, {'EmployeeID': Decimal('1011'), 'Employee Name': 'Betty Grof'}, {'EmployeeID': Decimal('1012'), 'Employee Name': 'King of Ooo'}, {'EmployeeID': Decimal('1030'), 'Employee Name': 'Huntress Wizard'}, {'EmployeeID': Decimal('1006'), 'Employee Name': 'BMO'}, {'EmployeeID': Decimal('1031'), 'Employee Name': 'Forest Spirit'}, {'EmployeeID': Decimal('1016'), 'Employee Name': 'Fern'}, {'EmployeeID': Decimal('1033'), 'Employee Name': 'Joshua '}, {'EmployeeID': Decimal('1003'), 'Employee Name': 'Bonnibel Bubblegum'}, {'EmployeeID': Decimal('1024'), 'Employee Name': 'Death'}, {'EmployeeID': Decimal('1020'), 'Employee Name': 'Gob'}, {'EmployeeID': Decimal('1027'), 'Employee Name': 'Prismo'}, {'EmployeeID': Decimal('1019'), 'Employee Name': 'Grob'}, {'EmployeeID': Decimal('1028'), 'Employee Name': 'Gunter'}, {'EmployeeID': Decimal('1013'), 'Employee Name': 'Hunson Abadeer'}]

df = pd.DataFrame(data)
df.head(5)

您的 output 看起来像:

    EmployeeID  Employee Name
0   1007        Lumpy Space Princess
1   1021        Glob
2   1029        Snail
3   1038        Banana Guard #216
4   1010        Martin Mertens

如果需要,您甚至可以创建 HTML:

df.head().to_html()

Output:

'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>EmployeeID</th>\n      <th>Employee Name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1007</td>\n      <td>Lumpy Space Princess</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1021</td>\n      <td>Glob</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1029</td>\n      <td>Snail</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1038</td>\n      <td>Banana Guard #216</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1010</td>\n      <td>Martin Mertens</td>\n    </tr>\n  </tbody>\n</table>'

暂无
暂无

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

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