Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbeff93

Browse files
author
y-p
committed
ENH: rework console encoding detection in fmt.print_config
1 parent 4c337f5 commit cbeff93

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pandas/core/format.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,31 @@ def __init__(self):
10991099
self.date_dayfirst = False
11001100
self.date_yearfirst = False
11011101
self.multi_sparse = True
1102-
self.encoding = sys.getdefaultencoding()
1103-
if self.encoding == 'ascii':
1104-
self.encoding = 'UTF8'
1102+
self.encoding = self.detect_encoding()
1103+
1104+
def detect_encoding(self):
1105+
"""
1106+
Try to find the most capable encoding supported by the console.
1107+
slighly modified from the way IPython handles the same issue.
1108+
"""
1109+
import locale
1110+
1111+
encoding = None
1112+
try:
1113+
encoding=sys.stdin.encoding
1114+
except AttributeError:
1115+
pass
1116+
1117+
if not encoding or encoding =='ascii': # try again for something better
1118+
try:
1119+
encoding = locale.getpreferredencoding()
1120+
except Exception:
1121+
pass
1122+
1123+
if not encoding: # when all else fails. this will usually be "ascii"
1124+
encoding = sys.getdefaultencoding()
1125+
1126+
return encoding
11051127

11061128
def reset(self):
11071129
self.__init__()

0 commit comments

Comments
 (0)