Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions rows/plugins/plugin_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from __future__ import unicode_literals

from io import BytesIO, BufferedReader
from io import open as io_open

import six
import unicodecsv
Expand All @@ -28,18 +29,17 @@
sniffer = unicodecsv.Sniffer()


class NotNullBytesWrapper(BufferedReader):

def read(self, *args, **kwargs):
data = super().read(*args, **kwargs)
return data.replace(b'\x00', b'')
if six.PY2:

def readline(self, *args, **kwargs):
data = super().readline(*args, **kwargs)
return data.replace(b'\x00', b'')
class NotNullBytesWrapper(BufferedReader):

def read(self, *args, **kwargs):
data = super(NotNullBytesWrapper, self).read(*args, **kwargs)
return data.replace(b'\x00', b'')

if six.PY2:
def readline(self, *args, **kwargs):
data = super(NotNullBytesWrapper, self).readline(*args, **kwargs)
return data.replace(b'\x00', b'')

def discover_dialect(sample, encoding=None,
delimiters=(b',', b';', b'\t', b'|')):
Expand All @@ -61,6 +61,16 @@ def discover_dialect(sample, encoding=None,

elif six.PY3:

class NotNullBytesWrapper(BufferedReader):

def read(self, *args, **kwargs):
data = super().read(*args, **kwargs)
return data.replace(b'\x00', b'')

def readline(self, *args, **kwargs):
data = super().readline(*args, **kwargs)
return data.replace(b'\x00', b'')

def discover_dialect(sample, encoding, delimiters=(',', ';', '\t', '|')):
"""Discover a CSV dialect based on a sample size

Expand Down Expand Up @@ -116,7 +126,11 @@ def import_from_csv(filename_or_fobj, encoding='utf-8', dialect=None,
"""

filename, fobj = get_filename_and_fobj(filename_or_fobj, mode='rb')
fobj = NotNullBytesWrapper(fobj)

if six.PY2:
fobj = NotNullBytesWrapper(io_open(filename, mode='rb'))
elif six.PY3:
fobj = NotNullBytesWrapper(fobj)

if dialect is None:
dialect = discover_dialect(sample=read_sample(fobj, sample_size),
Expand Down