【问题标题】:UnicodeEncodeError while using aiohttp in python3.6在 python3.6 中使用 aiohttp 时出现 UnicodeEncodeError
【发布时间】:2018-10-23 02:14:16
【问题描述】:

我使用aiohttp请求我的url,但是不知道为什么会出现这个错误!!!!!

async def get_location_data(url):
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                data = await response.json() 
                return data 
    except Exception:
        return None

当我收到响应并想更改列表中的项目时,会发生此错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)

搜了这么多,有人说应该用response.text(encoding="utf-8) 或者response.json(encoding="utf-8)

我该如何解决这个错误?

【问题讨论】:

  • 堆栈跟踪会很有用

标签: encoding python-asyncio python-unicode unicode-string aiohttp


【解决方案1】:

正如其他人所说,使用await response.json(encoding="utf-8")

【讨论】:

  • 我又试了一次,我发现因为我们公司服务器的一些配置无法从json响应中获取unicode字符串。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 2013-03-22
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多