简体   繁体   中英

Read csv file in tornado web framework

I am a newbie to tornado web framework. Currently I am building a web application which should load the csv file and then process it. I tried using csv module in python and also tried the tabular module which is not helping me.

So, my question is, Is there any package or open source library which will read the csv file data and store it in some variable kind of, so that I can perform various operations on the file.

I am getting the following error:

raise ValueError, "Need formats argument"

My code is :

import re
import sys
import os.path
import tornado.httpserver
import webbrowser
import personalfile
import tabular as tb
import csv

try:
    import tornado.ioloop
    import tornado.web
except:
    print 'No tornado module installed, Please install the tornado from http://www.tornadoweb.org/'
    sys.exit()

SRC = os.path.dirname(__file__)

# Main Handler
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("static/template_upload.html")



class OutputHandler(tornado.web.RequestHandler):
    def post(self):
        template_file = self.get_argument('template_html', None)
        **data_file = self.get_argument('csvfile', None)
        data_file = csv.DictReader(data_file)**
        data_file = tb.tabarray(data_file, verbosity=0, headerlines=1)
        xhtmlOutput = personalfile.function([templatehtmlfile,data_file])

        self.render('output.xhtml', data = xhtmlOutput)


application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/output", OutputHandler)
], debug=True,
   static_path=os.path.join(SRC, "static")
)



if __name__ == "__main__":
    print open(os.path.join(SRC, 'static', 'intro.txt')).read()
    webbrowser.open('http://127.0.0.1:8888')

    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

That's not how the csv.DictReader object works [module docs] . You've only made the reader, not actually extracted any data. If you want all the data to be stored, you'll have to actually read it. For example:

>>> import csv
>>> with open("test.csv") as fp:
...     reader = csv.reader(fp)
...     data = list(reader)
... 
>>> data
[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]
>>> 
>>> with open("test.csv") as fp:
...     reader = csv.DictReader(fp)
...     data = list(reader)
... 
>>> data
[{'a': '1', 'c': '3', 'b': '2'}, {'a': '4', 'c': '6', 'b': '5'}]

Et cetera. Whether or not you actually need all the data at once will depend upon your code.

There is a mistake in my code. The files that you upload will be available from self.request.files

Reference : http://www.tornadoweb.org/documentation/overview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM