foresmac
foresmac

Reputation: 125

How to store a CSV file uploaded via Django into a blobstore on Google App Engine?

I'm trying to upload a CSV file via Django app into a blobstore file in Google App Engine. I'm running into a problem were dumping the file as uploaded could end up with the wrong newlines. So, I need to open the uploaded file in python's universal newlines mode. The Django documentation suggests that I can use .open() on a File object, setting a new mode.

Here's my proposed solution:

        filename = files.blobstore.create(mime_type='text/csv')
        csvfile = request.FILES.get('csvfile')

        with files.open(filename, 'a') as output_file:
            with csvfile.open(mode='rU') as input_file:
                output_file.write(input_file.readline())

('files' is part of App Engine's API)

This seems right to me, but I wanted to know if anyone else has come under a similar situation and a better solution?

Upvotes: 1

Views: 531

Answers (1)

Jorge Salcedo
Jorge Salcedo

Reputation: 101

I already manage to do this. You have to iterate the uploaded file

filename = files.blobstore.create(mime_type='text/csv')
csvfile = request.FILES.get('csvfile')

with files.open(filename, 'a') as output_file: 
    writer = csv.writer(output_file, quoting=csv.QUOTE_NONE)
    for row in csv.reader(csvfile.read().splitlines()):
         writer.writerow(row)

files.finalize(filename)

Hope it works

Upvotes: 1

Related Questions