Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

CSV import doesn't work because "iterator should return strings, not bytes" #7

Open
rhunwicks opened this issue Jan 26, 2017 · 1 comment

Comments

@rhunwicks
Copy link

On DjangoCMS 3.2.5, Django 1.8, Python 2.7 with DjangoCMS Table from master, I can't get the CSV import to work because the CSV reader errors with "iterator should return strings, not bytes" in TableForm.clean_csv_upload().

Based on http://stackoverflow.com/questions/16243023/how-to-resolve-iterator-should-return-strings-not-bytes, the following works for me:

from io import TextIOWrapper


class TableForm(ModelForm):

    def clean_csv_upload(self):
        if self.cleaned_data['csv_upload']:

           # Make sure we decode the file to text
           encoding = self.cleaned_data['csv_upload'].charset if self.cleaned_data['csv_upload'].charset else 'utf-8'
           f = TextIOWrapper(self.cleaned_data['csv_upload'].file, encoding=encoding)
           csv_reader = csv.reader(f, dialect='excel')
@rhunwicks
Copy link
Author

Should I create a Merge Request?

rhunwicks added a commit to kimetrica/djangocms-table that referenced this issue Jan 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant