我正在尝试将希伯来字母插入我的数据库(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;我怀疑这是一条红鲱鱼。
有关python的更多notes。