Using Web form to Upload CSV File issue

737 views
Skip to first unread message

Elvin

unread,
Dec 4, 2009, 3:23:06 PM12/4/09
to Google App Engine
Hello, All!
Please help me with this issue!!!
I'm new to GAE and Python and I'm trying to make function to upload
CSV files using the Web form rather that bulkuploader.
I'm try to utilize csv.reader to parse the CSV but I get an incorrect
result, instead of reading each row individually, it treats each
character as an row:
['0'] ['.'] ['0'] ['5']['', ''] ['0'] ['.'] ['2'] etc etc... where
each item between the square bracket is actually from one row (ie. a
newline)

Here us my Python code:
class importEvents (webapp.RequestHandler):
def post(self):
fileReader = csv.reader(self.request.get('csv'),
dialect='excel')
for row in fileReader:
self.response.out.write(row)


And HTML code:
<form name="importFile" action="/importFile/importData"
enctype="multipart/form-data" method="post">
<div>CSV File(Comma Separated Value) File*: <input type="file"
name="csv"/></div>
<div><input type="submit" value="Upload"></div>
</form>


Please tell me what am I doing wrong here?
Thank you!





Ikai L (Google)

unread,
Dec 8, 2009, 2:40:50 PM12/8/09
to google-a...@googlegroups.com
You're getting this issue because csv.reader's first argument takes an object that supports the iterator interface, as documented here:

http://docs.python.org/library/csv.html

The call to open() in that document returns a file, not a String instance: http://docs.python.org/library/functions.html#open

The call self.request.get('csv') returns a String. When you iterate over a string, you iterate over the characters, not the lines. You can see the difference here:

class ProcessUpload(webapp.RequestHandler):
   def post(self):
     self.response.out.write(self.request.get('csv'))
     file = open(os.path.join(os.path.dirname(__file__), 'sample.csv'))
     self.response.out.write(file)
    
     # Iterating over a file
     fileReader = csv.reader(file)

     for row in fileReader:
       self.response.out.write(row)

     # Iterating over a string      
     fileReader = csv.reader(self.request.get('csv'))

     for row in fileReader:
       self.response.out.write(row)







--

You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.





--
Ikai Lan
Developer Programs Engineer, Google App Engine
Reply all
Reply to author
Forward
0 new messages