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

我有一个python boto3应用,正在flask中运行。主要目标是将列表检索格式化为人类可读。比如说 我想让列表返回为:

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],其中arr[1,1] = “1001”, arr[1,2] = “Finn”, arr[2,1] = “1002”, arr[2,2] = “Jake”, 等等。这样做的方向对吗?我甚至不知道这样做是否可行,或者说这根本不是DynamoDB数据的读取方式。

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

谢谢你。

解决方案:

你可以使用 大熊猫 包。

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)

你的输出会是这样的:

    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()

输出:

'<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>'

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何在concourse中链接yaml文件?

2022-9-10 2:01:24

未分类

将水平范围转换为数组并打印

2022-9-10 2:01:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索