I am trying to create a stock graph but im having some issues like, the y axis shows the numbers instead of the dates and the main issue is i want to change the format of strings in a list. They are dates but have been converted into strings.
Here is the data;
{"rates":{"2020-05-04":{"TRY":7.0308901481,"USD":1.0},"2020-03-25":{"TRY":6.4350235522,"USD":1.0},"2020-04-23":{"TRY":6.9428147048,"USD":1.0},"2020-03-31":{"TRY":6.5774917853,"USD":1.0},"2020-03-30":{"TRY":6.5683342396,"USD":1.0},"2020-04-22":{"TRY":6.9943866753,"USD":1.0},"2020-04-28":{"TRY":6.9977935092,"USD":1.0},"2020-03-27":{"TRY":6.4621481279,"USD":1.0},"2020-04-07":{"TRY":6.7581074874,"USD":1.0},"2020-04-09":{"TRY":6.7390264102,"USD":1.0},"2020-05-05":{"TRY":7.0889975099,"USD":1.0},"2020-04-01":{"TRY":6.6683430871,"USD":1.0},"2020-05-06":{"TRY":7.1414823725,"USD":1.0},"2020-05-11":{"TRY":7.0764966741,"USD":1.0},"2020-04-16":{"TRY":6.9347905952,"USD":1.0},"2020-04-14":{"TRY":6.7930311046,"USD":1.0},"2020-04-15":{"TRY":6.8873704485,"USD":1.0},"2020-03-23":{"TRY":6.5859222851,"USD":1.0},"2020-04-02":{"TRY":6.6628461397,"USD":1.0},"2020-04-17":{"TRY":6.9043278085,"USD":1.0},"2020-03-26":{"TRY":6.4117111374,"USD":1.0},"2020-03-24":{"TRY":6.4659227151,"USD":1.0},"2020-05-07":{"TRY":7.2143188352,"USD":1.0},"2020-04-21":{"TRY":6.9814524315,"USD":1.0},"2020-04-24":{"TRY":6.9782407407,"USD":1.0},"2020-04-03":{"TRY":6.7033843301,"USD":1.0},"2020-04-27":{"TRY":6.9825838555,"USD":1.0},"2020-04-06":{"TRY":6.7774997683,"USD":1.0},"2020-05-08":{"TRY":7.1245965139,"USD":1.0},"2020-04-20":{"TRY":6.926335175,"USD":1.0},"2020-04-30":{"TRY":6.9859323281,"USD":1.0},"2020-04-29":{"TRY":6.988286294,"USD":1.0},"2020-04-08":{"TRY":6.7885199154,"USD":1.0}},"start_at":"2020-03-23","base":"USD","end_at":"2020-05-12"}
I tried something like this,and many more things but it didn't work;
gunler = []
[gunler.append(key) for key in veri["rates"].keys()]
sorted_gunler = (datetime.datetime.strptime(i, "%Y-%M-%d") for i in gunler)
print(sorted_gunler)
The output was:
<generator object main.<locals>.<genexpr> at 0x7fcd0f0a8f68>
Tried this also:
new_list = [datetime.datetime.strptime(str(i), '%Y-%m-%d').strftime('%m-%d-%Y') for i in gunler]
Output:
['2020-05-04', '2020-03-25', '2020-04-23', '2020-03-31', '2020-03-30', '2020-04-22', '2020-04-28', '2020-03-27', '2020-04-07', '2020-04-09', '2020-05-05', '2020-04-01', '2020-05-06', '2020-05-11', '2020-04-16', '2020-04-14', '2020-04-15', '2020-03-23', '2020-04-02', '2020-04-17', '2020-03-26', '2020-03-24', '2020-05-07', '2020-04-21', '2020-04-24', '2020-04-03', '2020-04-27', '2020-04-06', '2020-05-08', '2020-04-20', '2020-04-30', '2020-04-29', '2020-04-08']
Then i came up with;
veri = r.json()
gunler = []
[gunler.append(key) for key in veri["rates"].keys()]
sorted(gunler, key=lambda x: datetime.datetime.strptime(x,'%Y-%M-%d'))
print(gunler)
It gave me the same output:
['2020-05-04', '2020-03-25', '2020-04-23', '2020-03-31', '2020-03-30', '2020-04-22', '2020-04-28', '2020-03-27', '2020-04-07', '2020-04-09', '2020-05-05', '2020-04-01', '2020-05-06', '2020-05-11', '2020-04-16', '2020-04-14', '2020-04-15', '2020-03-23', '2020-04-02', '2020-04-17', '2020-03-26', '2020-03-24', '2020-05-07', '2020-04-21', '2020-04-24', '2020-04-03', '2020-04-27', '2020-04-06', '2020-05-08', '2020-04-20', '2020-04-30', '2020-04-29', '2020-04-08']
But i want something like;
['2020-05-04','2020-05-05','2020-05-06'....]
The code works fine, but the result is not correct;
def main(): #the main section
anaBirim = input('\n' + 'Ana para birimini(kod cinsinden) giriniz: ').upper() #firstVal
ikincilBirim = input('İkincil para birimini(kod cinsinden) giriniz: ').upper() #secondVal
if anaBirim in para_birimleri and ikincilBirim in para_birimleri:
gun = int(input("Geriye dönük kaç günlük verinin hesaplanmasını istiyorsunuz: "))
today = datetime.date.today()
geriye_donuk = today - datetime.timedelta(days=gun)
url = ''.join(['https://api.exchangeratesapi.io/history?start_at={}&end_at={}&base={}&symbols={},{}'.format(geriye_donuk,today,anaBirim,anaBirim,ikincilBirim)])
ans = input("Ücüncü bir para birimi karşılaştırmak ister misiniz? E/H \n Cevap: ").upper()
r = requests.get(url)
veri = r.json()
prices = []
[prices.append(value) for value in veri["rates"].values()]
price_one = []
price_two = []
for price in prices:
p_one = price["{}".format(anaBirim)]
price_one.append(p_one)
p_two = price["{}".format(ikincilBirim)]
price_two.append(p_two)
if ans == "H":
plt.figure()
# ana birimin karşısındaki değeri
plt.plot(price_two,color="red")
plt.xlabel("{}\'nin {} günlük süreçteki değişimi".format(ikincilBirim,gun))
plt.ylabel("Değer")
plt.title('{} {}\'nin {} olarak karşılığı '.format(int(price_one[0]),anaBirim,ikincilBirim))
plt.show()
Any help would be greatly appreciated.