python, aiohttp, AssertionError: data argument must be byte-ish (<class 'str'>)

python, aiohttp, AssertionError: data argument must be byte-ish (<class 'str'>)

我收到标题中显示的错误。这是回溯:

2020-01-13T05:03:49.967604+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/aiohttp_debugtoolbar/middlewares.py", line 57, in toolbar_middleware
2020-01-13T05:03:49.967606+00:00 app[web.1]:     return (yield from handler(request))
2020-01-13T05:03:49.967608+00:00 app[web.1]:   File "/app/handlers/utils/web.py", line 139, in inner
2020-01-13T05:03:49.967610+00:00 app[web.1]:     return await func(request, prepare_response, filepath, **func_kwargs)
2020-01-13T05:03:49.967612+00:00 app[web.1]:   File "/app/handlers/csv2html.py", line 37, in csv2html
2020-01-13T05:03:49.967614+00:00 app[web.1]:     await response.write(reader.to_html())
2020-01-13T05:03:49.967616+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/aiohttp/web_response.py", line 416, in write
2020-01-13T05:03:49.967618+00:00 app[web.1]:     "data argument must be byte-ish (%r)" % type(data)
2020-01-13T05:03:49.967620+00:00 app[web.1]: AssertionError: data argument must be byte-ish (<class 'str'>)
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2020-01-13T05:03:49.967602+00:00 app[web.1]:     response = await handler(request)
2020-01-13T05:03:49.967604+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/aiohttp_debugtoolbar/middlewares.py", line 57, in toolbar_middleware
2020-01-13T05:03:49.967606+00:00 app[web.1]:     return (yield from handler(request))
2020-01-13T05:03:49.967608+00:00 app[web.1]:   File "/app/handlers/utils/web.py", line 139, in inner
2020-01-13T05:03:49.967610+00:00 app[web.1]:     return await func(request, prepare_response, filepath, **func_kwargs)
2020-01-13T05:03:49.967612+00:00 app[web.1]:   File "/app/handlers/csv2html.py", line 37, in csv2html
2020-01-13T05:03:49.967614+00:00 app[web.1]:     await response.write(reader.to_html())
2020-01-13T05:03:49.967616+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/aiohttp/web_response.py", line 416, in write
2020-01-13T05:03:49.967618+00:00 app[web.1]:     "data argument must be byte-ish (%r)" % type(data)
2020-01-13T05:03:49.967620+00:00 app[web.1]: AssertionError: data argument must be byte-ish (<class 'str'>)

该应用是另一个应用的api。它使用 aiohttp 并托管在 heroku。被调用的这个特定函数应该将 csv 转换为 html.

如果您愿意,我可以显示更多代码,但我认为没有必要。如我错了请纠正我。有人能帮忙吗?提前致谢。

37/app/handlers/csv2html.pyawait response.write(reader.to_html()) 替换为 await response.write(reader.to_html().encode("utf-8"))