Upload and parse csv file with “universal newline” in python on Google App Engine

后端 未结 2 2079
再見小時候
再見小時候 2020-12-16 17:48

I\'m uploading a csv/tsv file from a form in GAE, and I try to parse the file with python csv module.

Like describe here, uploaded files in GAE are strings.
So I

2条回答
  •  青春惊慌失措
    2020-12-16 18:03

    How about:

    file = self.request.get('catalog')
    file  = '\n'.join(file.splitlines())
    catalog = csv.reader(StringIO.StringIO(file),dialect=csv.excel_tab)
    

    or as pointed out in the comments, csv.reader() supports input from a list, so:

    file = self.request.get('catalog')
    catalog = csv.reader(file.splitlines(),dialect=csv.excel_tab)
    

    or if in the future request.get supports read modes:

    file = self.request.get('catalog', 'rU')
    catalog = csv.reader(StringIO.StringIO(file),dialect=csv.excel_tab)
    

提交回复
热议问题