gpt4 book ai didi

python - Unicode解码错误: 'ascii' codec can't decode byte 0x96 in position 10: ordinal not in range(128)

转载 作者:行者123 更新时间:2023-11-29 00:46:41 28 4
gpt4 key购买 nike

如何在不出现 UnicodeDecodeError 的情况下从 UTF-8 编码的 MySQL 数据库中获取数据?我正在使用 Python 和 HTML 模板制作网站。这是我用来从数据库中获取内容的代码,在我将数据库的编码切换为 UTF-8 之前它似乎工作正常:

@app.route("/songs")
def content_database_song():
c = connect_db()
c.execute("
SELECT * FROM Tracks
JOIN Artists USING (ArtistID)
JOIN Albums USING (AlbumID)
JOIN Songs USING (SongID)
ORDER BY UPPER(SoName), UPPER(AlTitle)
")
songslist = []
rows = c.fetchall()
for row in rows:
songslist.append(row)
return render_template("/song-index.html", songslist = songslist)

这是完整的回溯:

UnicodeDecodeError
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 10: ordinal not in range(128)

Traceback(最近调用最后)

File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1306, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1294, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1292, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1062, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1060, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line 1047, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/samuelbradshaw/Sites/praises/index.py", line 59, in content_database_song
return render_template("/song-index.html", songslist = songslist)
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/templating.py", line 121, in render_template
context, ctx.app)
File "/Library/Python/2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/templating.py", line 105, in _render
rv = template.render(context)
File "/Library/Python/2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/samuelbradshaw/Sites/praises/templates/song-index.html", line 1, in top-level template code
{% extends "database-nav.html" %}
File "/Users/samuelbradshaw/Sites/praises/templates/database-nav.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/Users/samuelbradshaw/Sites/praises/templates/layout.html", line 26, in top-level template code
{% block content %}{% endblock %}
File "/Users/samuelbradshaw/Sites/praises/templates/database-nav.html", line 13, in block "content"
{% block subcontent %}
File "/Users/samuelbradshaw/Sites/praises/templates/song-index.html", line 47, in block "subcontent"
<strong>Related Scriptures:</strong> {% if song.SoRelatedScriptures != "" %}{{song.SoRelatedScriptures}}{% else %}None{% endif %}<br>
File "/Library/Python/2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/_markupsafe/_native.py", line 21, in escape
return Markup(unicode(s)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 10: ordinal not in range(128)

最佳答案

我只需要调整一部分代码——它位于上面发布的代码片段中引用的 connect_db() 方法中。我改变了这个:

def connect_db():
global conn
conn = mdb.connect(dbinfo.server, dbinfo.username, dbinfo.password, dbinfo.database)
return conn.cursor(mdb.cursors.DictCursor)

为此:

def connect_db():
global conn
conn = mdb.connect(dbinfo.server, dbinfo.username, dbinfo.password, dbinfo.database, charset='utf8', use_unicode=True)
return conn.cursor(mdb.cursors.DictCursor)

连接时注意 charset='utf8', use_unicode=True。这就是我将数据库切换到 Unicode 后必须更改的全部内容! :)

关于python - Unicode解码错误: 'ascii' codec can't decode byte 0x96 in position 10: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com