2016-04-28 244 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'),因爲它看起來像序列化然後反序列化數據。