Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload and read csv file in django using csv.DictReader?

Tags:

python

csv

django

I am reading csv file through upload and trying to store all values in a list

def upload(request):
    paramFile = request.FILES['file'].read()
    data = csv.DictReader(paramFile)
    list1 = []
    for row in data:
        list1.append(row)

    print list1

file.csv

12345,abcdef

output

[{'1': '', None: ['']}, {'1': '2'}]

I want to append all values in list1

like image 760
shashisp Avatar asked Jul 11 '14 18:07

shashisp


People also ask

How import and read CSV file in Django?

Uploading CSV file: First create HTML form to upload the csv file. Use below code for the same. Important: Do not forget to include enctype="multipart/form-data" in form. Add a URL in URLpatterns.

What is the difference between CSV reader and CSV DictReader?

csv. Reader() allows you to access CSV data using indexes and is ideal for simple CSV files. csv. DictReader() on the other hand is friendlier and easy to use, especially when working with large CSV files.

What does CSV DictReader do in Python?

Python CSV DictReader The csv. DictReader class operates like a regular reader but maps the information read into a dictionary. The keys for the dictionary can be passed in with the fieldnames parameter or inferred from the first row of the CSV file.

Can I use CSV as database in Django?

You can import any models or other parts of your Django project to use in these scripts”. So, we import both the Films and Genre models and the csv Python builtin module in the script.


3 Answers

in Python 3, to get the correct type (String not bytes) without reading the full file into memory you can use a generator to decode line by line:

def decode_utf8(input_iterator):
    for l in input_iterator:
        yield l.decode('utf-8')

def upload(request):
    reader = csv.DictReader(decode_utf8(request.FILES['file']))
    for row in reader:
        print(row)
like image 169
mattwarren Avatar answered Sep 28 '22 01:09

mattwarren


This should work if you're using Python 3.

file = request.FILES['file'] 
decoded_file = file.read().decode('utf-8').splitlines()
reader = csv.DictReader(decoded_file)
for row in reader:
    # Get each cell value based on key-value pair. 
    # Key will always be what lies on the first row.

We can use the list that splitlines() creates. splitlines() is called because csv.DictReader expects "any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable".

like image 41
Jibu James Avatar answered Sep 28 '22 01:09

Jibu James


You have two problems:

  • You are passing a string to the constructor of DictReader. You must pass an iterable object that gives the individual lines in the input (a string is iterable, but will give each character one at a time). Luckily, an UploadedFile object (like those in the FILES dictionary) are already file-like objects that support iteration, so just do this:

    data = csv.DictReader(request.FILES['file'])
    
  • Your input data only has one line. DictReader will use that line for the column "headers", which will become the key in the resulting dictionaries. You will then have no data left! It looks like you don't want a DictReader, just a regualar reader:

    data = csv.reader(request.FILES['file'])
    
like image 29
Jamie Cockburn Avatar answered Sep 28 '22 02:09

Jamie Cockburn