Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,17 @@ def _next_line(self):
next(self.data)

while True:
orig_line = next(self.data)
try:
orig_line = next(self.data)
except csv.Error as e:
if 'NULL byte' in str(e):
raise csv.Error(
'NULL byte detected. This byte '
'cannot be processed in Python\'s '
'native csv library at the moment, '
'so please pass in engine=\'c\' instead.')
else:
raise
line = self._check_comments([orig_line])[0]
self.pos += 1
if (not self.skip_blank_lines and
Expand Down
16 changes: 16 additions & 0 deletions pandas/io/tests/parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,3 +1503,19 @@ def test_memory_map(self):

out = self.read_csv(mmap_file, memory_map=True)
tm.assert_frame_equal(out, expected)

def test_null_byte_char(self):
# see gh-2741
data = '\x00,foo'
cols = ['a', 'b']

expected = DataFrame([[np.nan, 'foo']],
columns=cols)

if self.engine == 'c':
out = self.read_csv(StringIO(data), names=cols)
tm.assert_frame_equal(out, expected)
else:
msg = "NULL byte detected"
with tm.assertRaisesRegexp(csv.Error, msg):
self.read_csv(StringIO(data), names=cols)