@@ -40,6 +40,7 @@ def __init__(self, cell_value, cell_location):
40
40
except ImportError :
41
41
from UserDict import UserDict # pylint: disable=F0401
42
42
43
+
43
44
def convert_type (type_string , value , timezone = pytz .timezone ('UTC' )):
44
45
if value == '' or value is None :
45
46
return None
@@ -446,18 +447,29 @@ class CSVInput(SpreadsheetInput):
446
447
encoding = 'utf-8'
447
448
448
449
def get_sheet_headings (self , sheet_name ):
450
+ sheet_configuration = self .sheet_configuration [self .sheet_names_map [sheet_name ]]
451
+ configuration_line = 1 if sheet_configuration else 0
452
+ if not sheet_configuration :
453
+ sheet_configuration = self .base_configuration
454
+ if not self .use_configuration :
455
+ sheet_configuration = {}
456
+ skip_rows = sheet_configuration .get ("skipRows" , 0 )
457
+ if sheet_configuration .get ("ignore" ):
458
+ # returning empty headers is a proxy for no data in the sheet.
459
+ return []
460
+
449
461
if sys .version > '3' : # If Python 3 or greater
450
462
with open (os .path .join (self .input_name , sheet_name + '.csv' ), encoding = self .encoding ) as main_sheet_file :
451
463
r = csvreader (main_sheet_file )
452
- for row in enumerate (r ):
453
- # Just return the first row
454
- return row [ 1 ]
464
+ for num , row in enumerate (r ):
465
+ if num == ( skip_rows + configuration_line ):
466
+ return row
455
467
else : # If Python 2
456
468
with open (os .path .join (self .input_name , sheet_name + '.csv' )) as main_sheet_file :
457
469
r = csvreader (main_sheet_file , encoding = self .encoding )
458
- for row in enumerate (r ):
459
- # Just return the first row
460
- return row [ 1 ]
470
+ for num , row in enumerate (r ):
471
+ if num == ( skip_rows + configuration_line ):
472
+ return row
461
473
462
474
def read_sheets (self ):
463
475
sheet_file_names = os .listdir (self .input_name )
@@ -472,21 +484,55 @@ def read_sheets(self):
472
484
except ValueError :
473
485
pass
474
486
self .sub_sheet_names = sheet_names
487
+ self .sheet_names_map = OrderedDict ((sheet_name , sheet_name ) for sheet_name in sheet_names )
475
488
self .configure_sheets ()
476
489
490
+ def generate_rows (self , dictreader , sheet_name ):
491
+ sheet_configuration = self .sheet_configuration [self .sheet_names_map [sheet_name ]]
492
+ configuration_line = 1 if sheet_configuration else 0
493
+ if not sheet_configuration :
494
+ sheet_configuration = self .base_configuration
495
+ if not self .use_configuration :
496
+ sheet_configuration = {}
497
+
498
+ skip_rows = sheet_configuration .get ("skipRows" , 0 )
499
+ header_rows = sheet_configuration .get ("headerRows" , 1 )
500
+ for i in range (0 , configuration_line + skip_rows ):
501
+ next (dictreader .reader )
502
+ fieldnames = dictreader .fieldnames
503
+ for i in range (0 , header_rows - 1 ):
504
+ next (dictreader .reader )
505
+ for line in dictreader :
506
+ yield OrderedDict ((fieldname , line [fieldname ]) for fieldname in fieldnames )
507
+
508
+ def get_sheet_configuration (self , sheet_name ):
509
+ if sys .version > '3' : # If Python 3 or greater
510
+ with open (os .path .join (self .input_name , sheet_name + '.csv' ), encoding = self .encoding ) as main_sheet_file :
511
+ r = csvreader (main_sheet_file )
512
+ heading_row = next (r )
513
+ else : # If Python 2
514
+ with open (os .path .join (self .input_name , sheet_name + '.csv' )) as main_sheet_file :
515
+ r = csvreader (main_sheet_file , encoding = self .encoding )
516
+ heading_row = next (r )
517
+ if heading_row [0 ] == '#' :
518
+ return heading_row [1 :]
519
+ return []
520
+
521
+
522
+
477
523
def get_sheet_lines (self , sheet_name ):
478
524
if sys .version > '3' : # If Python 3 or greater
479
525
# Pass the encoding to the open function
480
526
with open (os .path .join (self .input_name , sheet_name + '.csv' ), encoding = self .encoding ) as main_sheet_file :
481
527
dictreader = DictReader (main_sheet_file )
482
- for line in dictreader :
483
- yield OrderedDict (( fieldname , line [ fieldname ]) for fieldname in dictreader . fieldnames )
528
+ for row in self . generate_rows ( dictreader , sheet_name ) :
529
+ yield row
484
530
else : # If Python 2
485
531
# Pass the encoding to DictReader
486
532
with open (os .path .join (self .input_name , sheet_name + '.csv' )) as main_sheet_file :
487
533
dictreader = DictReader (main_sheet_file , encoding = self .encoding )
488
- for line in dictreader :
489
- yield OrderedDict (( fieldname , line [ fieldname ]) for fieldname in dictreader . fieldnames )
534
+ for row in self . generate_rows ( dictreader , sheet_name ) :
535
+ yield row
490
536
491
537
492
538
class XLSXInput (SpreadsheetInput ):
0 commit comments