File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ from urllib .request import urlopen
2+ from urllib .error import HTTPError
3+
4+ import pandas as pd
5+
6+ _PD_READERS = {
7+ 'json' : pd .read_json ,
8+ 'csv' : pd .read_csv
9+ }
10+
11+ def _get_format (url ):
12+ """Gives back the format of the file from url
13+
14+ WARNING: It might break. Trying to find a better way.
15+ """
16+ return url .split ('.' )[- 1 ]
17+
18+ def _fetch (url ):
19+ """Downloads the file from the given url as a Pandas DataFrame
20+
21+ Parameters
22+ ----------
23+ url : string
24+ URL of the file to be downloaded
25+
26+ Returns
27+ -------
28+ pd.DataFrame
29+ Data in the format of a DataFrame
30+
31+ Raises
32+ ------
33+ NotImplementedError
34+ Raises when an unsupported file format is given as an URL
35+ """
36+ try :
37+ ext = _get_format (url )
38+ reader = _PD_READERS [ext ]
39+ df = reader (urlopen (url ).read ())
40+ except KeyError :
41+ raise NotImplementedError ('File format not implemented' )
42+ return df
You can’t perform that action at this time.
0 commit comments