0

I am trying to import a csv file from a webpage using flask. I am able to import the data from the csv file and return the data as json. However, I would like to print only the first sample from the data. I have attached my code and the flask error below. The csv file I am using is csvfile. The returned json data looks something like this

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

Please advice what is wrong with the script.

`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.
Kvik
  • 101
  • 2
  • 6

1 Answers1

0

I guess the issue here is with jsonify.

According to the docs:

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

Creates a Response with the JSON representation of the given arguments with an application/json mimetype.

(See also this answer.)

You'd typically use it to send a json to a client (like in an API, for instance) and let it handle the protocol stuff.

When writing this:

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

it looks like you expect it to return just the json data, not a full response.

Did you try to print a and see what's in there? You may also want to print request.get_array(field_name='file') as it looks like you are serializing then deserializing the data.

Community
  • 1
  • 1
Jérôme
  • 13,328
  • 7
  • 56
  • 106