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

时间:2017-05-05 12:03:25

标签: python mysql unicode flask

我正在尝试将希伯来字母插入我的数据库(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中,我可以看到希伯来字母)。

谢谢!

< ----------使用EXTRA CODE编辑------------>

        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),))

&lt; ---------------- UPDATES ------------&gt;

  • 我发现了我的第一个问题 - 我试图得到len(str(request.form ['MalfunctionOpener']))这提供了这个乱码××××××并给了我一个错误

  • 我也试图进入到我的数据库再版(的Request.Form [ 'MalfunctionOpener']),它给我这种类型的u'\ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \ u203e \'(仅举例)但我不知道如何从数据库中获取并将其更改为希伯来语。

1 个答案:

答案 0 :(得分:0)

שלו×是&#34; Mojibake&#34;对于שלו,请参阅Trouble with utf8 characters; what I see is not what I stored

\u203eË的Unicode;我怀疑这是一只红鲱鱼。

python上有更多notes