@@ -2469,26 +2469,7 @@ def _next_line(self):
2469
2469
next (self .data )
2470
2470
2471
2471
while True :
2472
- try :
2473
- orig_line = next (self .data )
2474
- except csv .Error as e :
2475
- msg = str (e )
2476
-
2477
- if 'NULL byte' in str (e ):
2478
- msg = ('NULL byte detected. This byte '
2479
- 'cannot be processed in Python\' s '
2480
- 'native csv library at the moment, '
2481
- 'so please pass in engine=\' c\' instead' )
2482
-
2483
- if self .skipfooter > 0 :
2484
- reason = ('Error could possibly be due to '
2485
- 'parsing errors in the skipped footer rows '
2486
- '(the skipfooter keyword is only applied '
2487
- 'after Python\' s csv library has parsed '
2488
- 'all rows).' )
2489
- msg += '. ' + reason
2490
-
2491
- raise csv .Error (msg )
2472
+ orig_line = self ._next_iter_line ()
2492
2473
line = self ._check_comments ([orig_line ])[0 ]
2493
2474
self .pos += 1
2494
2475
if (not self .skip_blank_lines and
@@ -2510,6 +2491,44 @@ def _next_line(self):
2510
2491
self .buf .append (line )
2511
2492
return line
2512
2493
2494
+ def _next_iter_line (self , ** kwargs ):
2495
+ """
2496
+ Wrapper around iterating through `self.data` (CSV source).
2497
+
2498
+ When a CSV error is raised, we check for specific
2499
+ error messages that allow us to customize the
2500
+ error message displayed to the user.
2501
+
2502
+ Parameters
2503
+ ----------
2504
+ kwargs : Keyword arguments used to customize the error message.
2505
+ """
2506
+
2507
+ try :
2508
+ return next (self .data )
2509
+ except csv .Error as e :
2510
+ msg = str (e )
2511
+
2512
+ if 'NULL byte' in msg :
2513
+ msg = ('NULL byte detected. This byte '
2514
+ 'cannot be processed in Python\' s '
2515
+ 'native csv library at the moment, '
2516
+ 'so please pass in engine=\' c\' instead' )
2517
+ elif 'newline inside string' in msg :
2518
+ msg = ('EOF inside string starting with '
2519
+ 'line ' + str (kwargs ['row_num' ]))
2520
+ raise Exception (msg )
2521
+
2522
+ if self .skipfooter > 0 :
2523
+ reason = ('Error could possibly be due to '
2524
+ 'parsing errors in the skipped footer rows '
2525
+ '(the skipfooter keyword is only applied '
2526
+ 'after Python\' s csv library has parsed '
2527
+ 'all rows).' )
2528
+ msg += '. ' + reason
2529
+
2530
+ raise csv .Error (msg )
2531
+
2513
2532
def _check_comments (self , lines ):
2514
2533
if self .comment is None :
2515
2534
return lines
@@ -2688,7 +2707,6 @@ def _rows_to_cols(self, content):
2688
2707
return zipped_content
2689
2708
2690
2709
def _get_lines (self , rows = None ):
2691
- source = self .data
2692
2710
lines = self .buf
2693
2711
new_rows = None
2694
2712
@@ -2703,14 +2721,14 @@ def _get_lines(self, rows=None):
2703
2721
rows -= len (self .buf )
2704
2722
2705
2723
if new_rows is None :
2706
- if isinstance (source , list ):
2707
- if self .pos > len (source ):
2724
+ if isinstance (self . data , list ):
2725
+ if self .pos > len (self . data ):
2708
2726
raise StopIteration
2709
2727
if rows is None :
2710
- new_rows = source [self .pos :]
2711
- new_pos = len (source )
2728
+ new_rows = self . data [self .pos :]
2729
+ new_pos = len (self . data )
2712
2730
else :
2713
- new_rows = source [self .pos :self .pos + rows ]
2731
+ new_rows = self . data [self .pos :self .pos + rows ]
2714
2732
new_pos = self .pos + rows
2715
2733
2716
2734
# Check for stop rows. n.b.: self.skiprows is a set.
@@ -2726,21 +2744,17 @@ def _get_lines(self, rows=None):
2726
2744
try :
2727
2745
if rows is not None :
2728
2746
for _ in range (rows ):
2729
- new_rows .append (next (source ))
2747
+ new_rows .append (next (self . data ))
2730
2748
lines .extend (new_rows )
2731
2749
else :
2732
2750
rows = 0
2751
+
2733
2752
while True :
2734
- try :
2735
- new_rows .append (next (source ))
2736
- rows += 1
2737
- except csv .Error as inst :
2738
- if 'newline inside string' in str (inst ):
2739
- row_num = str (self .pos + rows )
2740
- msg = ('EOF inside string starting with '
2741
- 'line ' + row_num )
2742
- raise Exception (msg )
2743
- raise
2753
+ new_row = self ._next_iter_line (
2754
+ row_num = self .pos + rows )
2755
+ new_rows .append (new_row )
2756
+ rows += 1
2757
+
2744
2758
except StopIteration :
2745
2759
if self .skiprows :
2746
2760
new_rows = [row for i , row in enumerate (new_rows )
0 commit comments