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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将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">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>EmployeeID</th>
      <th>Employee Name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1007</td>
      <td>Lumpy Space Princess</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1021</td>
      <td>Glob</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1029</td>
      <td>Snail</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1038</td>
      <td>Banana Guard #216</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1010</td>
      <td>Martin Mertens</td>
    </tr>
  </tbody>
</table>'

以上是关于如何将Python Boto3中的DynamoDB扫描格式化为可人读的html?的主要内容,如果未能解决你的问题,请参考以下文章

无法使用Lambda Python 3.6 Boto3在现有DynamoDB表中放置新项目[关闭]

python--boto3 之 与dynamoDB 的基本交互,表的备份与恢复

在DynamoDB python boto3中执行update_item时出错

使用python将复杂对象保存到dynamoDb

使用带有全局二级索引的 boto3 在 dynamodb 上进行有条件的放置

用于 DynamoDB 查询的 Python 代码适用于 v3.6,但不适用于 python 2.7