1

I am uploading and importing a CSV file in Django. Each row has a "description" field which could have line breaks. Currently all the line breaks are lost when saving to Django TextField and this causes all the formatting to be lost. The user then has go to the web portal or admin console and manually format the text for the description field. This is really tedious and time consuming when you hundreds of records.

Is there a way to maintain formatting when importing a CSV file?

Currently I am using:

file = request.FILES['csv_file']

csv_file_data = [row for row in csv.reader(file.read().splitlines())]   
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
Mehtaji
  • 13
  • 3

2 Answers2

2

From here:

def splitkeepsep(s, sep):
    return reduce(lambda acc, elem: acc[:-1] + [acc[-1] + elem] if elem == sep else acc + [elem], re.split("(%s)" % re.escape(sep), s), [])

Hence:

file = request.FILES['csv_file']

# Updated to reflect OP's comments:
csv_file_data = [row for row in csv.reader(splitkeepsep(file.read(), '\n'), dialect=csv.excel_tab)]
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • The solution results in: "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?" The file that is being imported was created in excel and saved as csv. – Mehtaji Aug 24 '14 at 15:40
  • One thing I observed was saving the file as Windows CSV and then importing did not result in that exception. Also, a "\n" is replaced by a space. – Mehtaji Aug 24 '14 at 16:10
  • @Mehtaji: Ah! I see: sorry about that! Edited. You may also find this related question useful too: http://stackoverflow.com/questions/1875956/how-can-i-access-an-uploaded-file-in-universal-newline-mode – jrd1 Aug 24 '14 at 23:04
0

I had the same problem when using django-import-export.

I ended up overriding the import_field method (but do let me know if there is a better way):

# admin.py

from django.db.models.fields import TextField


class YourModelResource(resources.ModelResource):

    def import_field(self, field, obj, data, is_m2m=False):
        field_model = YourModel._meta.get_field(field.column_name)
        # keep linebreaks in TextField columns
        if type(field_model) == TextField:
            data[field.column_name] = data[field.column_name].replace('\n', '<br />\n')
        field.save(obj, data, is_m2m)
raphodn
  • 196
  • 1
  • 6