无法在Flask应用中编写CSV文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在Flask应用中编写CSV文件相关的知识,希望对你有一定的参考价值。
我在flask上创建了这个简单的应用程序,用户可以上传包含名称的csv文件,它将返回一个新的csv文件,该文件只有那些以S或M开头的名字。当我点击上传它应该下载新的文件,但我不知道我在哪里出错。可能在写下csv。请帮帮我。使用Python 3.x这是app.py
import os
import pandas as pd
import csv
from flask import Flask, request, redirect, url_for, render_template, send_from_directory
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/uploads/'
DOWNLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/downloads/'
ALLOWED_EXTENSIONS = {'csv'}
app = Flask(__name__, static_url_path="/static")
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 8 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
print('No file attached in request')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
print('No file selected')
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))
process_file(os.path.join(app.config['UPLOAD_FOLDER'], filename), filename)
return redirect(url_for('uploaded_file', filename=filename))
return render_template('index.html')
def process_file(path, filename):
update_file(path, filename)
def update_file(path, filename):
data= pd.read_csv(path)
work = data['first'].tolist()
work_final=[]
for item in work:
if(checkitems(item) == True):
work_final.append(item)
headers = {}
headers[str('content-type')] ='text/csv'
headers['Content_Disposition'] = 'attachment; filename='+filename +'.csv'
f = open(path + filename + '.csv' , 'wb')
writer = csv.writer(f)
writer.writerow(work_final)
f.close()
def checkitems(items):
if (items[0] == 'S' or items[0] == 'M'):
return True
else:
return False
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename, as_attachment=True)
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
这是模板index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Name Changer</title>
</head>
<body>
<div align="center">
<h1> Avinash Name Filter</h1>
<h2> Upload the CSV File here</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value=Upload>
</form>
</div>
</body>
</html>
请帮帮我。
我在这里附上了这个示例csv
seq,first,age,date
1,Bernice,36,08/22/2013
2,Dale,63,07/18/1964
3,Oscar,53,05/02/1943
4,Elijah,57,12/13/1919
5,Chad,48,04/26/1958
6,Mabel,22,01/22/1916
7,Clayton,29,01/06/1901
8,James,47,07/01/1944
9,Olga,65,02/09/2020
10,Helena,23,03/11/2045
11,Jessie,31,01/07/2032
12,Ann,58,10/20/2058
13,Sallie,32,12/05/2009
14,Brandon,34,07/16/2067
15,Millie,61,07/10/2051
16,Travis,25,12/07/1997
17,Charlie,39,01/08/2001
18,Lester,19,01/31/1922
19,Charles,59,05/01/1929
20,Dennis,27,06/28/2050
21,Sean,28,07/12/2013
22,Jeffrey,33,10/04/2001
23,Evelyn,26,06/27/2048
24,Nell,32,03/05/1981
25,Gavin,30,04/08/2031
26,Mitchell,20,03/12/1977
27,Ruby,37,07/01/2024
28,Wesley,30,06/27/2060
29,Wesley,31,01/30/1978
30,Mina,49,10/20/2056
31,Daisy,43,04/05/1998
32,Martin,59,02/24/1964
33,Lee,39,01/24/2015
34,Timothy,58,11/20/2028
35,Rosetta,42,08/11/2020
36,Darrell,51,11/11/1984
37,Bess,42,06/04/1956
38,Estelle,52,06/08/2042
39,Aaron,65,09/03/2021
40,Jon,50,11/15/2000
41,Cory,32,06/14/1901
42,Leroy,63,07/02/2003
43,Grace,19,11/24/1929
44,Dennis,63,07/31/1927
45,Christopher,24,11/21/1968
46,Nancy,24,03/05/1948
47,Edith,65,08/31/1961
48,Raymond,39,01/10/1948
49,Agnes,22,11/28/1921
50,Steve,47,04/09/2052
这是csv - https://a.uguu.se/DMHQAUYuqqFh_test.csv的链接
答案
我测试了你的应用程序,没有update_file()函数,它在上传后下载文件。所以,你在处理.csv文件的某个地方犯了一个错误。如果你可以添加示例csv文件,这将是伟大的(抱歉答案,但不能发表评论beucase我是stackoverflow的新手)。
编辑:
我发现了错误。你应该把:
f = open(path + filename + '.csv' , 'w')
你是在二进制模式下阅读,这就是你的数组无法写入字符串的原因。此外,从该文件中,csv读取器读取的第一件事是1,2,3,4 ...而不是S或M.检查出来,您将在新的CSV文件中有有效写入
以上是关于无法在Flask应用中编写CSV文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Cordova android 应用程序中使用 javascript 创建 csv 或 Excel 文件