Using Web form to Upload CSV File issue

723 weergaven
Naar het eerste ongelezen bericht

Elvin

ongelezen,
4 dec 2009, 15:23:0604-12-2009
aan 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)

ongelezen,
8 dec 2009, 14:40:5008-12-2009
aan 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
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten