turicas / rows

A common, beautiful interface to tabular data, no matter the format
GNU Lesser General Public License v3.0
869 stars 134 forks source link

uploaded file #325

Open mheijink opened 5 years ago

mheijink commented 5 years ago

I have a simple csv with ; as delimiter. rows.import_from_csv("test.csv") works great if I read it from a disk file.

But when reading the same file when it is uploaded is not working. rows.import_from_csv(request.FILES['file'])

discover_dialect falls back to 'excel'

turicas commented 5 years ago

Can you share file contents or an equivalent code so I can reproduce here?

mheijink commented 5 years ago

OrgDefinedID;Final Adjusted Grade test1;46.28 test2;80.28 test3;61,2 % TrainingStudent01; test4; 10 test5; 11 test6; 12

turicas commented 5 years ago

@mheijink I'm trying to reproduce here using io.BytesIO (is it equivalent to your request.FILES['file'] object?) but couldn't. Could you please help creating a reproducible code? I've tried:

import io
import rows

filename = "test.csv"
table = rows.import_from_csv(filename)
with open(filename, mode="rb") as fobj:
    data = fobj.read()
table2 = rows.import_from_csv(io.BytesIO(data))
assert list(table) == list(table2)  # no AssertionError raised, so tables have the same content
mheijink commented 5 years ago

request.FILES['file'] is a of type django.core.files.uploadedfile.InMemoryUploadedFile If I change it like this it works

`` import rows from io import BytesIO

    fp = request.FILES['bestand']

    for chunk in fp.chunks():
        logger.debug(type(chunk))
        logger.debug(chunk)

        rows_data = rows.import_from_csv(BytesIO(chunk))
        for d in rows_data:
            logger.debug(d)
            logger.debug(d[0])
            logger.debug(d[1])

``