我使用带有烧瓶框架的python2.7,我尝试从Html页面中的textarea获取文本并将其存储在文本文件中,如果文本用英文写成,它可以成功运行,但我需要让它适用于阿拉伯语文字。 我试了很多没有结果的解决方案
我能做什么!!这是代码。
# -*- coding: cp1256-*-
from flask import Flask, render_template, request
import jinja2
import os
import codecs
os.chdir("C:\Python27")
app = Flask(__name__)
@app.route("/")
def hello():
return render_template("razan.html")
@app.route('/submit', methods=['POST'])
def submit_textarea():
text = request.form.get("text")
with open('aaa.txt','w') as outfile:
outfile.write(text)
return render_template("razan.html")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int("3000"), debug=True)
这是HTML代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="cp1256">
</head>
<body>
<h1>Enter some text</h1>
<form action="submit" id="textform" method="post" >
<textarea name="text" type="text"> مرحبا </textarea>
<button type="submit" value="submit_textarea()" > submt</button>
</form>
</body>
</html>
它给了我这个错误:
UnicodeEncodeError:&#39; ascii&#39;编解码器不能编码位置1-6中的字符:序数不在范围内(128)
答案 0 :(得分:0)
以unicode
读取文件import codecs
outfile = codecs.open('aaa.txt', 'w', 'utf-8')
outfile.write(text)