2016-04-28 305 views
0

我想从一个网页使用烧瓶导入一个csv文件。我能够从csv文件中导入数据并将数据作为json返回。但是,我想仅打印数据中的第一个样本。下面附上我的代码和烧瓶错误。我正在使用的csv文件是csvfile。返回的JSON数据看起来像这样Flask-Python从csv文件导入数据

{ 
 
    "result": [ 
 
    [ 
 
     "0.011223", 
 
     "0.018274", 
 
     "0.071568", 
 
     "0.3407", 
 
     "0.50367", 
 
     "0.63498", 
 
     "0.45607", 
 
     "0.39945", 
 
     "0.27201", 
 
     "0.23569", 
 
     "0.29102", 
 
     "0.15327", 
 
     "0.095266", 
 
     "0.059091", 
 
     "0.014877", 
 
     "0.00010369", 
 
     "0.049384", 
 
     "0.12681", 
 
     "0.24325", 
 
     "0.30725", 
 
     "0.4259", 
 
     "0.56476", 
 
     "0.606", 
 
     "0.1001", 
 
     "0.5427", 
 
     "0.63342", 
 
     "0.62526", 
 
     "0.59211", 
 
     "0.59013", 
 
     "0.50669", 
 
     "0.42666", 
 
     "0.29487", 
 
     "0.20149",

请咨询什么是错的脚本。

`from flask import Flask, request, jsonify, render_template 
from flask.ext import excel 
import json, csv 

app=Flask(__name__) 
app.debug = True 

@app.route("/upload", methods=['GET', 'POST']) 
def upload_file(): 
    if request.method == 'POST': 
     a= jsonify({"result": request.get_array(field_name='file')}) 
     entries = json.loads(a) 
     entry=entries['result'][0] 
     return "<h2>'entry=%f'</h2>"%entry 
    return ''' 
    <!doctype html> 
    <title>Upload an excel file</title> 
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1> 
    <form action="" method=post enctype=multipart/form-data><p> 
    <input type=file name=file><input type=submit value=Upload> 
    </form> 
     ''' 


    if __name__ == "__main__": 
    app.run()` 

TypeError 
 
TypeError: expected string or buffer 
 

 
Traceback (most recent call last) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1836, in __call__ 
 
return self.wsgi_app(environ, start_response) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1820, in wsgi_app 
 
response = self.make_response(self.handle_exception(e)) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1403, in handle_exception 
 
reraise(exc_type, exc_value, tb) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1817, in wsgi_app 
 
response = self.full_dispatch_request() 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request 
 
rv = self.handle_user_exception(e) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1381, in handle_user_exception 
 
reraise(exc_type, exc_value, tb) 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request 
 
rv = self.dispatch_request() 
 
File "C:\Users\Vikrant\Desktop\Flask1\chap1\lib\site-packages\flask\app.py", line 1461, in dispatch_request 
 
return self.view_functions[rule.endpoint](**req.view_args) 
 
File "C:\Users\Vikrant\Desktop\Flask1\Flaskr\flaskr_t2.py", line 12, in upload_file 
 
entries = json.loads(a) 
 
File "c:\python27\Lib\json\__init__.py", line 339, in loads 
 
return _default_decoder.decode(s) 
 
File "c:\python27\Lib\json\decoder.py", line 364, in decode 
 
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
 
TypeError: expected string or buffer 
 
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. 
 
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. 
 

 
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: 
 

 
dump() shows all variables in the frame 
 
dump(obj) dumps all that's known about the object 
 
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

回答

0

我想这里的问题是jsonify

根据the docs

flask.json.jsonify(*指定参数时,** kwargs)

创建具有的给定的参数与应用程序/ JSON的mimetype JSON表示的响应。

(也this answer见。)

您通常会使用它来一个JSON发送给客户端(在API中一样,例如),让它处理协议的东西。

当写这样的:

a= jsonify({"result": request.get_array(field_name='file')}) 
    entries = json.loads(a) 

它看起来像你期望它仅返回JSON数据,不是一个完整的响应。

你试过打印a看看里面有什么吗?您可能还想打印request.get_array(field_name='file'),因为它看起来像序列化然后反序列化数据。