0

I am trying to read a csv file line-by-line in my views.py file. The csv file is structured as follows:

xk, ab, cd

11, 20, 30

31, 27, 35

etc

if request.method == "POST":
    pdb.set_trace()
    form = FormName(request.POST, request.FILES)
    file = request.FILES['csv'].read()
    if form.is_valid():       
        try:
            reader = csv.DictReader(file)
            for row in reader:
                xk = row['xk'] 

Before executing the last line above, when I print 'row' in pdb, I get {'x': 'k'}. However, I should get "11, 20, 30" (or something similar with that data, since DictReader is supposed to automatically use the first line as a header. When I print the file in pdb I get:

'xk,ab,cd\n11,20,30\n31,27,35' etc. How can I properly read the csv file?

1 Answers1

0

Resolved in comments:

try the same without the .read() method, like in this post – PRMoureu

request.FILES['csv'] returns an UploadedFile object, that seems to fit your dictReader approach – PRMoureu

^^That worked. Thanks!! – user7518095

stovfl
  • 14,998
  • 7
  • 24
  • 51