Skip to content

Commit 3f9b6a2

Browse files
committed
Add headers option in read_csv() for Python3
1 parent c296a93 commit 3f9b6a2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pandas/io/common.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ def urlopen(*args, **kwargs):
150150
"""
151151
import urllib.request
152152

153-
return urllib.request.urlopen(*args, **kwargs)
153+
# Request class is only available in Python3, which
154+
# allows headers to be specified
155+
if hasattr(urllib.request, 'Request'):
156+
r = urllib.request.urlopen(urllib.request.Request(*args, **kwargs))
157+
else:
158+
r = urllib.request.urlopen(*args, **kwargs)
159+
160+
return r
154161

155162

156163
def is_fsspec_url(url: FilePathOrBuffer) -> bool:
@@ -176,6 +183,7 @@ def get_filepath_or_buffer(
176183
compression: CompressionOptions = None,
177184
mode: ModeVar = None, # type: ignore[assignment]
178185
storage_options: StorageOptions = None,
186+
headers: dict = {}
179187
) -> IOargs[ModeVar, EncodingVar]:
180188
"""
181189
If the filepath_or_buffer is a url, translate and return the buffer.
@@ -251,7 +259,7 @@ def get_filepath_or_buffer(
251259
raise ValueError(
252260
"storage_options passed with file object or non-fsspec file path"
253261
)
254-
req = urlopen(filepath_or_buffer)
262+
req = urlopen(filepath_or_buffer, headers=headers)
255263
content_encoding = req.headers.get("Content-Encoding", None)
256264
if content_encoding == "gzip":
257265
# Override compression based on Content-Encoding header

pandas/io/parsers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,10 @@ def _read(filepath_or_buffer: FilePathOrBuffer, kwds):
432432
encoding = re.sub("_", "-", encoding).lower()
433433
kwds["encoding"] = encoding
434434
compression = kwds.get("compression", "infer")
435+
headers = kwds.get("headers", {})
435436

436437
ioargs = get_filepath_or_buffer(
437-
filepath_or_buffer, encoding, compression, storage_options=storage_options
438+
filepath_or_buffer, encoding, compression, storage_options=storage_options, headers=headers
438439
)
439440
kwds["compression"] = ioargs.compression
440441

@@ -599,6 +600,7 @@ def read_csv(
599600
memory_map=False,
600601
float_precision=None,
601602
storage_options: StorageOptions = None,
603+
headers={}
602604
):
603605
# gh-23761
604606
#
@@ -686,6 +688,7 @@ def read_csv(
686688
infer_datetime_format=infer_datetime_format,
687689
skip_blank_lines=skip_blank_lines,
688690
storage_options=storage_options,
691+
headers=headers
689692
)
690693

691694
return _read(filepath_or_buffer, kwds)

0 commit comments

Comments
 (0)