Accès en local si le fichier existe sinon accès à distance et création d'une copie locale

parent e306ac2f
...@@ -59,7 +59,60 @@ ...@@ -59,7 +59,60 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'incidence-PAY-3.csv'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Récupération du nom du fichier à partir du path d'accès aux données\n",
"from os.path import basename\n",
"\n",
"filename = basename(data_url)\n",
"filename"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"incidence-PAY-3.csv doesn't exist\n"
]
}
],
"source": [
"# Si le fichier existe en local on le lit directement\n",
"# Sinon on l'accède avec son URL et on le sauvegarde en local\n",
"\n",
"# La première ligne du fichier CSV est un commentaire, que nous ignorons en précisant skiprows=1.\n",
"import os\n",
"\n",
"if os.path.exists(filename):\n",
" print(filename, \"exist\")\n",
" raw_data = pd.read_csv(filename, skiprows=1)\n",
"else:\n",
" print(filename, \"doesn't exist\")\n",
" raw_data = pd.read_csv(data_url, skiprows=1)\n",
" raw_data.to_csv(filename)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -1024,13 +1077,12 @@ ...@@ -1024,13 +1077,12 @@
"[1847 rows x 10 columns]" "[1847 rows x 10 columns]"
] ]
}, },
"execution_count": 3, "execution_count": 19,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"raw_data = pd.read_csv(data_url, skiprows=1)\n",
"raw_data" "raw_data"
] ]
}, },
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment