@@ -433,7 +433,7 @@ def _parse_excel(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
433
433
434
434
epoch1904 = self .book .datemode
435
435
436
- def _parse_cell (cell_contents ,cell_typ ):
436
+ def _parse_cell (cell_contents , cell_typ ):
437
437
"""converts the contents of the cell into a pandas
438
438
appropriate object"""
439
439
@@ -482,7 +482,7 @@ def _parse_cell(cell_contents,cell_typ):
482
482
483
483
ret_dict = False
484
484
485
- #Keep sheetname to maintain backwards compatibility.
485
+ # Keep sheetname to maintain backwards compatibility.
486
486
if isinstance (sheetname , list ):
487
487
sheets = sheetname
488
488
ret_dict = True
@@ -492,7 +492,7 @@ def _parse_cell(cell_contents,cell_typ):
492
492
else :
493
493
sheets = [sheetname ]
494
494
495
- #handle same-type duplicates.
495
+ # handle same-type duplicates.
496
496
sheets = list (set (sheets ))
497
497
498
498
output = {}
@@ -517,7 +517,7 @@ def _parse_cell(cell_contents,cell_typ):
517
517
should_parse [j ] = self ._should_parse (j , parse_cols )
518
518
519
519
if parse_cols is None or should_parse [j ]:
520
- row .append (_parse_cell (value ,typ ))
520
+ row .append (_parse_cell (value , typ ))
521
521
data .append (row )
522
522
523
523
if header is not None :
@@ -541,68 +541,95 @@ def _parse_cell(cell_contents,cell_typ):
541
541
else :
542
542
return output [asheetname ]
543
543
544
-
545
544
def _parse_ods (self , sheetname = 0 , header = 0 , skiprows = None , skip_footer = 0 ,
546
545
index_col = None , has_index_names = None , parse_cols = None ,
547
546
parse_dates = False , date_parser = None , na_values = None ,
548
547
thousands = None , chunksize = None , convert_float = True ,
549
- ** kwds ):
550
-
551
- # sheetname can be index or string
552
- sheet = self .book .sheets [sheetname ]
553
-
554
- data = []
555
- should_parse = {}
556
- for i in range (sheet .nrows ()):
557
- row = []
558
- for j , cell in enumerate (sheet .row (i )):
559
-
560
- if parse_cols is not None and j not in should_parse :
561
- should_parse [j ] = self ._should_parse (j , parse_cols )
562
-
563
- if parse_cols is None or should_parse [j ]:
564
-
565
- if isinstance (cell .value , float ):
566
- value = cell .value
567
- if convert_float :
568
- # GH5394 - Excel and ODS 'numbers' are always floats
569
- # it's a minimal perf hit and less suprising
570
- # FIXME: this goes wrong when int(cell.value) returns
571
- # a long (>1e18)
572
- val = int (cell .value )
573
- if val == cell .value :
574
- value = val
575
- elif isinstance (cell .value , compat .string_types ):
576
- typ = cell .value_type
577
- # if typ == 'string':
578
- # value = cell.value
579
- if typ == 'date' or typ == 'time' :
580
- value = self ._parse_datetime (cell )
581
- else :
582
- value = cell .value
583
- elif isinstance (cell .value , bool ):
584
- value = cell .value
585
- # elif isinstance(cell.value, type(None)):
586
- # value = np.nan
587
- else :
588
- value = np .nan
548
+ verbose = False , ** kwds ):
549
+
550
+ def _parse_cell (cell ):
551
+ """converts the contents of the cell into a pandas
552
+ appropriate object"""
553
+ if isinstance (cell .value , float ):
554
+ value = cell .value
555
+ if convert_float :
556
+ # GH5394 - Excel and ODS 'numbers' are always floats
557
+ # it's a minimal perf hit and less suprising
558
+ # FIXME: this goes wrong when int(cell.value) returns
559
+ # a long (>1e18)
560
+ val = int (cell .value )
561
+ if val == cell .value :
562
+ value = val
563
+ elif isinstance (cell .value , compat .string_types ):
564
+ typ = cell .value_type
565
+ # if typ == 'string':
566
+ # value = cell.value
567
+ if typ == 'date' or typ == 'time' :
568
+ value = self ._parse_datetime (cell )
569
+ else :
570
+ value = cell .value
571
+ elif isinstance (cell .value , bool ):
572
+ value = cell .value
573
+ # elif isinstance(cell.value, type(None)):
574
+ # value = np.nan
575
+ else :
576
+ value = np .nan
577
+ return value
578
+
579
+ ret_dict = False
580
+
581
+ # Keep sheetname to maintain backwards compatibility.
582
+ if isinstance (sheetname , list ):
583
+ sheets = sheetname
584
+ ret_dict = True
585
+ elif sheetname is None :
586
+ sheets = self .sheet_names
587
+ ret_dict = True
588
+ else :
589
+ sheets = [sheetname ]
590
+
591
+ # handle same-type duplicates.
592
+ sheets = list (set (sheets ))
593
+
594
+ output = {}
595
+
596
+ for asheetname in sheets :
597
+ if verbose :
598
+ print ("Reading sheet %s" % asheetname )
589
599
590
- row .append (value )
600
+ # sheetname can be index or string
601
+ sheet = self .book .sheets [asheetname ]
591
602
592
- data .append (row )
603
+ data = []
604
+ should_parse = {}
605
+ for i in range (sheet .nrows ()):
606
+ row = []
607
+ for j , cell in enumerate (sheet .row (i )):
608
+
609
+ if parse_cols is not None and j not in should_parse :
610
+ should_parse [j ] = self ._should_parse (j , parse_cols )
593
611
594
- parser = TextParser (data , header = header , index_col = index_col ,
595
- has_index_names = has_index_names ,
596
- na_values = na_values ,
597
- thousands = thousands ,
598
- parse_dates = parse_dates ,
599
- date_parser = date_parser ,
600
- skiprows = skiprows ,
601
- skip_footer = skip_footer ,
602
- chunksize = chunksize ,
603
- ** kwds )
612
+ if parse_cols is None or should_parse [j ]:
613
+ row .append (_parse_cell (cell ))
614
+
615
+ data .append (row )
616
+
617
+ parser = TextParser (data , header = header , index_col = index_col ,
618
+ has_index_names = has_index_names ,
619
+ na_values = na_values ,
620
+ thousands = thousands ,
621
+ parse_dates = parse_dates ,
622
+ date_parser = date_parser ,
623
+ skiprows = skiprows ,
624
+ skip_footer = skip_footer ,
625
+ chunksize = chunksize ,
626
+ ** kwds )
627
+ output [asheetname ] = parser .read ()
604
628
605
- return parser .read ()
629
+ if ret_dict :
630
+ return output
631
+ else :
632
+ return output [asheetname ]
606
633
607
634
def _parse_datetime (self , cell ):
608
635
"""Parse the date or time from on ods cell to a datetime object.
@@ -616,7 +643,7 @@ def _parse_datetime(self, cell):
616
643
def _value2date (value ):
617
644
try :
618
645
return datetime .datetime .strptime (value , '%Y-%m-%d' )
619
- except ValueError :# , TypeError):
646
+ except ValueError : # , TypeError):
620
647
return datetime .datetime .strptime (value , '%Y-%m-%dT%H:%M:%S' )
621
648
622
649
# Technically it is not necessary to try to derive the date/time
@@ -650,7 +677,7 @@ def _value2date(value):
650
677
value = _value2date (cell .value )
651
678
elif cell .value_type == 'time' :
652
679
try :
653
- # FIXME: what if the decimal separator is a comma in the locale?
680
+ # FIXME: what if the decimal separator is a comma in locale?
654
681
value = datetime .datetime .strptime (cell .value , 'PT%HH%MM%S.%fS' )
655
682
except ValueError :
656
683
value = datetime .datetime .strptime (cell .value , 'PT%HH%MM%SS' )
@@ -664,9 +691,9 @@ def _print_ods_cellinfo(self, cell):
664
691
Cell attributes are documented here:
665
692
https://pythonhosted.org/ezodf/tableobjects.html#id2
666
693
"""
667
- print (' plaintext:' , cell .plaintext ()) # no formatting
694
+ print (' plaintext:' , cell .plaintext ()) # no formatting
668
695
# formatted, but what is difference with value?
669
- print ('display_form:' , cell .display_form ) # format, ?=plaintext
696
+ print ('display_form:' , cell .display_form ) # format, ?=plaintext
670
697
print (' value:' , cell .value ) # data handled
671
698
print (' value_type:' , cell .value_type ) # data type
672
699
print (' formula:' , cell .formula )
0 commit comments