When I write a date in a terminal, I execute my code like this:
python code.py " date"
For example python code.py 2017-05-14, but I have more than 1500 dates. I have saved them in a file.txt.
But I don't know how I can adapt parsing in my code to recuperate all the variables from the file
if __name__ == '__main__':
parser = argparse.ArgumentParser()
with open(...) as f:
for line in f:
<do something with line>
parser.add_argument("date", help="date format YYYY-MM-DD", type=str)
parser.add_argument("--output", help="csv output filepath",type=str)
args = parser.parse_args()
region = {
'France':'France',
'ACA':'Grand-Est',
'ALP':'Nouvelle-Aquitaine',
'ARA':'Auvergne-Rhônes-Alpes',
'BFC':'Bourgogne-Franche-Comté',
'BRE':'Bretagne',
'CEN':'Centre-Val de Loire',
'IDF':'Ile-de-France',
'LRM':'Occitanie',
'NPP':'Hauts-de-France',
'NOR':'Normandie',
'PLO':'Pays-de-Loire',
'PAC':'PACA',
}
# french date format...
datefr = args.date[-2:] + '/' + args.date[5:7] + '/' + args.date[:4]
if args.output:
output = args.output
else:
output = 'eco2mix-' + args.date + '.csv'
# grab all regions...
df = [grab_params({'region':k,'date':datefr}) for k in region.keys()]
df = pd.concat(df)
assert len(df) == 96*len(region.keys())
# NOTE : patching malformed date...
df['Date'] = [i if '-' in i else i[-4:]+'-'+i[3:5]+'-'+i[:2] for i in df['Date']]
assert len(set(df.Date)) == 1
# NOTE : remove - empty values by NaN
df.replace(to_replace = '-', value = np.NaN, inplace=True)
df.replace(to_replace = 'ND', value = np.NaN, inplace=True)
df.to_csv(output,index = False, encoding = 'UTF8')