Python 2.7 Unicode参数应为PYQRCODE
通过以下操作,将生成内存中的映像:Python 2.7 Unicode参数应为PYQRCODE,python-2.7,svg,unicode,Python 2.7,Svg,Unicode,通过以下操作,将生成内存中的映像: url = pyqrcode.create(user.get_totp_uri()) stream = StringIO() url.svg(stream, scale=3) return stream.getvalue().encode('utf-8'), 200, { 'Content-Type': 'image/svg+xml', 'Cache-Control': 'no-cache, no-store, must-revalidate'
url = pyqrcode.create(user.get_totp_uri())
stream = StringIO()
url.svg(stream, scale=3)
return stream.getvalue().encode('utf-8'), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
我得到以下错误:
File "E:\Data\pathsdb\flask\lib\site-packages\pyqrcode\builder.py", line 1118, in _svg
write_bytes(b'<?xml version="1.0" encoding="UTF-8"?>\n')
TypeError: unicode argument expected, got 'str'
文件“E:\Data\pathsdb\flask\lib\site packages\pyqrcode\builder.py”,第1118行,在svg中
写入字节(b'\n')
TypeError:应为unicode参数,得到'str'
有人知道我做错了什么吗 已解决:
使用以下代码:
url = pyqrcode.create(user.get_totp_uri())
stream = BytesIO()
url.svg(stream, scale=3)
return stream.getvalue(), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
应该使用BytesIO和drop编码