Flask-Python从csv文件导入数据

时间:2016-04-28 13:07:55

标签: python json csv flask

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

{
  "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.

1 个答案:

答案 0 :(得分:0)

我想这里的问题是jsonify

根据the docs

  

flask.json.jsonify(* args,** kwargs)

     

使用application / json mimetype创建一个具有给定参数的JSON表示的Response。

(另见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'),因为看起来您正在序列化然后反序列化数据。

相关问题