Skip to content

MAINT: Nicer error msg for NULL byte in read_csv #13859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2016
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)