Skip to content

Commit fbb6a1d

Browse files
committed
BUG: pass encoding to open in Python 3
1 parent 8610be5 commit fbb6a1d

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

pandas/core/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,13 @@ def __iter__(self):
626626
def next(self):
627627
return self.reader.next().encode("utf-8")
628628

629+
def _get_handle(path, mode, encoding=None):
630+
if py3compat.PY3: # pragma: no cover
631+
f = open(path, mode, encoding=encoding)
632+
else:
633+
f = open(path, mode)
634+
return f
635+
629636
if py3compat.PY3: # pragma: no cover
630637
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
631638
# ignore encoding

pandas/core/frame.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,7 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
932932
f = path_or_buf
933933
close = False
934934
else:
935-
if py3compat.PY3: # pragma: no cover
936-
f = open(path_or_buf, mode, encoding=encoding)
937-
else:
938-
f = open(path_or_buf, mode)
935+
f = com._get_handle(path_or_buf, mode, encoding=encoding)
939936
close = True
940937

941938
try:

pandas/io/parsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def read_csv(filepath_or_buffer, sep=',', header=0, index_col=None, names=None,
9797
else:
9898
try:
9999
# universal newline mode
100-
f = open(filepath_or_buffer, 'U')
100+
f = com._get_handle(filepath_or_buffer, 'U', encoding=encoding)
101101
except Exception: # pragma: no cover
102-
f = open(filepath_or_buffer, 'r')
102+
f = com._get_handle(filepath_or_buffer, 'r', encoding=encoding)
103103

104104
if delimiter is not None:
105105
sep = delimiter

0 commit comments

Comments
 (0)