【问题标题】:Getting UnicodeEncodeError while loading data from DynamoDB in python在 python 中从 DynamoDB 加载数据时获取 UnicodeEncodeError
【发布时间】:2018-07-16 13:44:32
【问题描述】:

我从 DynamoDB 获取数据时遇到的错误;

UnicodeEncodeError:“charmap”编解码器无法在位置 1761 编码字符“\u2019”:字符映射到

代码;

self.db = boto3.resource(
        'dynamodb',
        region_name=Config.AWS_DB_REGION_NAME,
        aws_access_key_id=Config.AWS_DB_ACCESS_KEY,
        aws_secret_access_key=Config.AWS_DB_SECRET_KEY
)
self.tableName = "test"

queryAll():
    self.actionTable = self.db.Table(self.tableName)

    query = self.actionTable.scan(
        ProjectionExpression='id, cityName, title, lat, lng',
        FilterExpression=Attr("type").eq("attraction")
    )

    return query

venues = queryAll()

print("Found Records: {}\n".format(str(venues['Count']) + "/" + 
str(venues['ScannedCount'])))
founds = list()

if venues['Count']:
 for i, item in enumerate(venues['Items']):
    if "lat" in item:
        founds.append(
            {
                "id": item["id"],
                "title": str(item["title"]),
                "location": str(item['lat']) + "," + str(item['lng'])
            })
print(founds)
Items = venues['Items']
exit()

【问题讨论】:

  • 请改一下标题,太脏了。请您提供完整的错误堆栈。
  • @Mr.J 英文不太好:(
  • 现在,看起来不错,请提供详细的错误堆栈。
  • @Mr.J,我添加到founds变量的数据写不出来
  • 上面代码中的哪一行导致了错误?我需要错误详细信息而不是源代码或输入

标签: python amazon-dynamodb


【解决方案1】:

用这个函数解决问题

.encode('ascii','ignore').decode()

使用;

"title": str(item["title"]).encode('ascii','ignore').decode(),

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2021-02-17
    • 2015-07-24
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2022-01-07
    相关资源
    最近更新 更多