"if-else" statements to see if the file exists, creation of a local file if False

parent a7e7e61d
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
"%matplotlib inline\n", "%matplotlib inline\n",
"import matplotlib.pyplot as plt\n", "import matplotlib.pyplot as plt\n",
"import pandas as pd\n", "import pandas as pd\n",
"import isoweek" "import isoweek\n",
"import os.path"
] ]
}, },
{ {
...@@ -28,12 +29,13 @@ ...@@ -28,12 +29,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"data_url = \"http://www.sentiweb.fr/datasets/incidence-PAY-3.csv\"\n", "data_url = \"http://www.sentiweb.fr/datasets/incidence-PAY-3.csv\"\n",
"data_local = \"C:\\\\Users\\\\lefevrej\\\\Documents\\\\training_repro\\\\local-file.csv\"" "path_local = \"C:\\\\Users\\\\lefevrej\\\\Documents\\\\training_repro\\\\\"\n",
"data_local = path_local+\"incidence-PAY-3.csv\""
] ]
}, },
{ {
...@@ -54,8 +56,14 @@ ...@@ -54,8 +56,14 @@
"| inc100_low | Estimation de la borne inférieure de l'IC95% du taux d'incidence du nombre de cas de consultation (en cas pour 100,000 habitants) |\n", "| inc100_low | Estimation de la borne inférieure de l'IC95% du taux d'incidence du nombre de cas de consultation (en cas pour 100,000 habitants) |\n",
"| inc100_up | Estimation de la borne supérieure de l'IC95% du taux d'incidence du nombre de cas de consultation (en cas pour 100,000 habitants) |\n", "| inc100_up | Estimation de la borne supérieure de l'IC95% du taux d'incidence du nombre de cas de consultation (en cas pour 100,000 habitants) |\n",
"| geo_insee | Code de la zone géographique concernée (Code INSEE) http://www.insee.fr/fr/methodes/nomenclatures/cog/ |\n", "| geo_insee | Code de la zone géographique concernée (Code INSEE) http://www.insee.fr/fr/methodes/nomenclatures/cog/ |\n",
"| geo_name | Libellé de la zone géographique (ce libellé peut être modifié sans préavis) |\n", "| geo_name | Libellé de la zone géographique (ce libellé peut être modifié sans préavis) |"
"\n", ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"L'ouverture du fichier en local est gérée par un essai d'ouverture du fichier: si le fichier n'existe pas ou s'il y a un problème durant la lecture, les données proviendront directement du téléchargement via l'URL des données dans un nouveau fichier local.\n",
"La première ligne du fichier CSV est un commentaire, que nous ignorons en précisant `skiprows=1`." "La première ligne du fichier CSV est un commentaire, que nous ignorons en précisant `skiprows=1`."
] ]
}, },
...@@ -1032,10 +1040,13 @@ ...@@ -1032,10 +1040,13 @@
} }
], ],
"source": [ "source": [
"try:\n", "file_exists = os.path.exists(data_local)\n",
"if file_exists: #return True if the file exists\n",
" raw_data = pd.read_csv(data_local, encoding = 'iso-8859-1', skiprows=1)\n", " raw_data = pd.read_csv(data_local, encoding = 'iso-8859-1', skiprows=1)\n",
"except ValueError:\n", "else:\n",
" raw_data = pd.read_csv(data_url, encoding = 'iso-8859-1', skiprows=1)\n", " raw_data = pd.read_csv(data_url, encoding = 'iso-8859-1', skiprows=1) #read the data from the URL\n",
" raw_data.to_csv(data_local, sep=',') #export/download data in the local file\n",
"\n",
"raw_data" "raw_data"
] ]
}, },
...@@ -2212,7 +2223,7 @@ ...@@ -2212,7 +2223,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7fbd7ebe70f0>" "<matplotlib.axes._subplots.AxesSubplot at 0x7f1363b201d0>"
] ]
}, },
"execution_count": 9, "execution_count": 9,
...@@ -2251,7 +2262,7 @@ ...@@ -2251,7 +2262,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7fbd7caf4588>" "<matplotlib.axes._subplots.AxesSubplot at 0x7f13619fe1d0>"
] ]
}, },
"execution_count": 10, "execution_count": 10,
...@@ -2355,7 +2366,7 @@ ...@@ -2355,7 +2366,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7fbd7ca57668>" "<matplotlib.axes._subplots.AxesSubplot at 0x7f1361995b00>"
] ]
}, },
"execution_count": 13, "execution_count": 13,
...@@ -2458,7 +2469,7 @@ ...@@ -2458,7 +2469,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7fbd7c9b06d8>" "<matplotlib.axes._subplots.AxesSubplot at 0x7f13618da5c0>"
] ]
}, },
"execution_count": 15, "execution_count": 15,
......
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