-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_currency_rate.py
41 lines (33 loc) · 1.33 KB
/
add_currency_rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python3
import csv
from datetime import datetime
def main(csv_file, input_file, output_file):
try:
# Read currency rates from CSV database
with open(csv_file, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
currency_rates = {row[0]: row[1] for row in csv_reader}
# Read dates from input file
with open(input_file, 'r') as file:
input_dates = file.readlines()
# Write results to output file
with open(output_file, 'w') as file:
file.write("Date,Currency Rate\n")
for input_date in input_dates:
input_date = input_date.strip()
currency_rate = currency_rates.get(input_date, "N/A")
file.write(f"{input_date},{currency_rate}\n")
print(f"Conversion completed. Results written to {output_file}")
except FileNotFoundError:
print(f"Error: File not found - {csv_file}, {input_file}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
import sys
if len(sys.argv) != 4:
print("Usage: add_currency_rate.py currency_rates.csv input_file output_file.csv")
else:
csv_file = sys.argv[1]
input_file = sys.argv[2]
output_file = sys.argv[3]
main(csv_file, input_file, output_file)