我正在尝试替换字符串并在Flask中转换上传文件的编码。我已经找到上传文件的代码,并且可以正常工作,但是我在替换字符串和转换编码时遇到问题。我已经检查了许多有关该问题的问题,但是可能由于我的英语水平不足而无法找到解决方案。
我想我无法获取通过Web表单上传的文件。我正在将文件上传到“ static / uploads”文件夹,并且可以在此处看到文件。我需要帮助来获取上载的文件,然后检测编码并替换字符并保存。
我正在使用Windows pc python 3.7。
import os
from chardet import detect
from app import app
from flask import render_template, flash, redirect, request
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['txt', 'srt'])
target_format = 'UTF-8'
outputDir = 'static/coverted'
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(filename, 'rb') as f:
raw_data = f.read()
source_format = detect(raw_data)['encoding']
with open(filename, 'rU', source_format) as sf, open(outputDir + '/' + filename, 'w',
encoding='UTF-8') as tf:
content = sf.read()
replaced_content = content.replace('Ý', 'İ').replace('ý', 'ı').replace('þ', 'ş').replace('ð', 'ğ').replace('Þ', 'Ş')
tf.write(replaced_content)
return redirect('/')
else:
flash('Allowed file types are txt,srt')
return redirect(request.url)
我得到的结果是:AttributeError:'FileNotFoundError:[Errno 2]没有这样的文件或目录:
答案 0 :(得分:0)
好的,我设法替换了内容,然后在上传后将其保存到文件中。 这是其他人需要的代码。
import os
from chardet import detect
from app import app
from flask import render_template, flash, redirect, request, send_from_directory, url_for
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['txt', 'srt'])
target_format = 'UTF-8'
outputDir = 'static/coverted'
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
with open(file_path, 'rb') as f:
raw_data = f.read()
source_format = detect(raw_data)['encoding']
print(source_format)
with open(file_path, 'rU', encoding=source_format) as sf:
content = sf.read()
replaced_content = content.replace('Ý', 'İ').replace('ý', 'ı').replace('þ', 'ş').replace('ð',
'ğ').replace(
'Þ', 'Ş')
print(file_path)
with open(file_path, 'w', encoding=target_format) as tf:
tf.write(replaced_content)
return redirect('/')
else:
flash('Allowed file types are txt,srt')
return redirect(request.url)