gpt4 book ai didi

python - UnicodeEncodeError - 尝试使用 Flask 插入 SQL 希伯来语字母

转载 作者:行者123 更新时间:2023-11-29 19:01:58 24 4
gpt4 key购买 nike

我正在尝试将希伯来字母插入我的数据库(mysql)使用 request.form['some_input']

这是我的init.py 的代码:

    if request.method == "POST":
malfunction_opener = request.form['MalfunctionOpener']
malfunction_handler = request.form['malfunctionHandler']
system = request.form['system']
unit = request.form['unit']

我知道发生的事情是关于对我获得的信息进行编码/解码,但我无法完成这项工作。我也尝试过 get_data,将其编码为 utf-8,获取它的字符串值,但没有成功。

在许多其他帖子中,我看到需要添加:

# -*- coding: utf-8 -*-

我也这么做了。

当我尝试插入任何希伯来字母时,我总是收到此错误,例如:“שלום”

2017-05-02 17:35:23,930 - Rotating Log - ERROR - 'ascii' codec can't encode 
characters in position 0-3: ordinal not in range(128)
2017-05-02 17:35:23,931 - Rotating Log - ERROR -
request.form['malfunctionDescription'] value is: שלו×
2017-05-02 17:35:23,932 - Rotating Log - ERROR -
Traceback (most recent call last):
File "/var/www/WebApp/WebApp/__init__.py", line 166, in new_malfunction
thwart(malfunction_description), thwart(malfunction_solution),
thwart(malfunction_summary),))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
ordinal not in range(128)

也许它是相关的,但我遇到的另一个问题是,当我将希伯来字母直接插入mysql并将其导入我的网站时,我得到这些问号。(但在 mysql 中我可以看到希伯来字母)。

谢谢!

<---------- 使用额外代码进行编辑 ------------>

        if len(str(request.form['malfunctionDescription'])) <= 0:
flash("You must enter a description.")
return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
else:
malfunction_description = request.form['malfunctionDescription']

if status == 0 and len(str(request.form['malfunctionSolution'])) <= 0:
flash("You must enter a solution.")
return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
else:
malfunction_solution = request.form['malfunctionSolution']

if status == 0 and len(str(request.form['malfunctionSummary'])) <= 0:
flash("You must enter a summary.")
return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
else:
malfunction_summary = request.form['malfunctionSummary']

x = c.execute("SELECT * FROM malfunctions WHERE malfunctionDescription = (%s)",
(malfunction_description,))

if int(x) > 0:
flash("You have already enter this malfunction")
return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)

else:
c.execute(
"INSERT INTO malfunctions (status, openingTime, closingTime, malfunctionOpener, malfunctionHandler, system, unit, rank, malfunctionDescription, malfunctionSolution, malfunctionSummary) VALUES (%s,%s,%s, %s, %s, %s, %s, %s, %s, %s, %s)",
(thwart(status), thwart(opening_date), thwart(closing_date), thwart(malfunction_opener),
thwart(malfunction_handler), thwart(system), thwart(unit), thwart(rank),
thwart(malfunction_description), thwart(malfunction_solution), thwart(malfunction_summary),))

<---------------- 更新 ------------>

  • 我发现了我的第一个问题 - 我试图获取 len(str(request.form['MalfunctionOpener'])) ,它提供了这个胡言乱语 ×©×œ×•× 并给了我一个错误

  • 我还尝试输入我的数据库 repr(request.form['MalfunctionOpener']) ,这给了我这种类型的 u'\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\' (只是一个例子),但我不知道如何从数据库中获取它并将其更改为希伯来语。

最佳答案

שלו×שלו 的“Mojibake”,请参阅 Trouble with utf8 characters; what I see is not what I stored

\u203eË 的 Unicode;我怀疑这是转移注意力。

更多 notes在Python上。

关于python - UnicodeEncodeError - 尝试使用 Flask 插入 SQL 希伯来语字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804465/

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