Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active September 28, 2023 15:08
Show Gist options
  • Save rg3915/85f1b600dd08619f76d94b7e41c3d04e to your computer and use it in GitHub Desktop.
Save rg3915/85f1b600dd08619f76d94b7e41c3d04e to your computer and use it in GitHub Desktop.
Read csv InMemoryUploadedFile Django
import csv
import io
def import_csv(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
# Read csv file InMemoryUploadedFile
file = myfile.read().decode('utf-8')
reader = csv.DictReader(io.StringIO(file))
# Generate a list comprehension
data = [line for line in reader]
save_data(data) # Not implemented
return HttpResponseRedirect(reverse('product:product_list'))
template_name = 'product_import.html'
return render(request, template_name)
@Naveenchandra07
Copy link

I am getting a "Format None cannot be imported." error when tried to process a InMemoryUploadedFile using above method. Can you please help to know what is the issue ?

@XioNoX
Copy link

XioNoX commented Oct 22, 2020

Thank you, that was useful.

@lasododo
Copy link

this one was really useful, thanks alot for it ^^
for ppl, who are looking for reading the file line by line, here is good SO answer ;)

@bmrobin
Copy link

bmrobin commented Feb 16, 2021

thanks! this helped me a lot 👏

@BrunoGehlen
Copy link

Thank you, mate!

@raissalst
Copy link

thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment