@@ -1378,6 +1378,12 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
1378
1378
self .has_mi_columns = (isinstance (obj .columns , MultiIndex ) and
1379
1379
not self .tupleize_cols )
1380
1380
1381
+ # in Python 3, decode bytes to str so strings print without b''
1382
+ if compat .PY3 :
1383
+ self .bytes_encoding = (encoding or get_option ("display.encoding" ))
1384
+ else :
1385
+ self .bytes_encoding = None
1386
+
1381
1387
# validate mi options
1382
1388
if self .has_mi_columns :
1383
1389
if cols is not None :
@@ -1387,6 +1393,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
1387
1393
if cols is not None :
1388
1394
if isinstance (cols , Index ):
1389
1395
cols = cols .to_native_types (na_rep = na_rep ,
1396
+ bytes_encoding = self .bytes_encoding ,
1390
1397
float_format = float_format ,
1391
1398
date_format = date_format ,
1392
1399
quoting = self .quoting )
@@ -1399,6 +1406,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
1399
1406
cols = self .obj .columns
1400
1407
if isinstance (cols , Index ):
1401
1408
cols = cols .to_native_types (na_rep = na_rep ,
1409
+ bytes_encoding = self .bytes_encoding ,
1402
1410
float_format = float_format ,
1403
1411
date_format = date_format ,
1404
1412
quoting = self .quoting )
@@ -1506,6 +1514,8 @@ def _save_header(self):
1506
1514
else :
1507
1515
encoded_labels = []
1508
1516
1517
+ self ._bytes_to_str (encoded_labels )
1518
+
1509
1519
if not has_mi_columns :
1510
1520
encoded_labels += list (write_cols )
1511
1521
@@ -1565,6 +1575,7 @@ def _save_chunk(self, start_i, end_i):
1565
1575
for i in range (len (self .blocks )):
1566
1576
b = self .blocks [i ]
1567
1577
d = b .to_native_types (slicer = slicer , na_rep = self .na_rep ,
1578
+ bytes_encoding = self .bytes_encoding ,
1568
1579
float_format = self .float_format ,
1569
1580
decimal = self .decimal ,
1570
1581
date_format = self .date_format ,
@@ -1575,13 +1586,22 @@ def _save_chunk(self, start_i, end_i):
1575
1586
self .data [col_loc ] = col
1576
1587
1577
1588
ix = data_index .to_native_types (slicer = slicer , na_rep = self .na_rep ,
1589
+ bytes_encoding = self .bytes_encoding ,
1578
1590
float_format = self .float_format ,
1579
1591
decimal = self .decimal ,
1580
1592
date_format = self .date_format ,
1581
1593
quoting = self .quoting )
1582
1594
1583
1595
lib .write_csv_rows (self .data , ix , self .nlevels , self .cols , self .writer )
1584
1596
1597
+ def _bytes_to_str (self , values ):
1598
+ """Modify values list by decoding bytes to str."""
1599
+ if self .bytes_encoding :
1600
+ for ii , value in enumerate (values ):
1601
+ if isinstance (value , bytes ):
1602
+ values [ii ] = value .decode (self .bytes_encoding )
1603
+
1604
+
1585
1605
# from collections import namedtuple
1586
1606
# ExcelCell = namedtuple("ExcelCell",
1587
1607
# 'row, col, val, style, mergestart, mergeend')
0 commit comments