In one program I wrote, I saved the average price of BTC each day into a .txt file with this format:
Date,Price
"Jun 05, 2018",7567.330
"Jun 04, 2018",7618.500
"Jun 03, 2018",7676.170
"Jun 02, 2018",7590.080
"Jun 01, 2018",7521.070
"May 31, 2018",7450.160
"May 30, 2018",7438.120
...
In my code, I have a list of dates where I have to match the date in the list to a date in the textfile and find the average BTC price for that day. I am planning to save all of the dates and prices into a list each, called "coin_dates" and "coin_prices" respectively.
I tried to model my code after how a json dictionary is typically opened the difference being that I took away the "json.load(f)":
def initial_price(df):
with open(df, "r") as f:
coin_dates = [d["Date"] for d in f]
coin_prices = [d["Price"] for d in f]
initial_price("btc.txt")
but I got this error:
Traceback (most recent call last):
File "getICOdate.py", line 158, in <module>
initial_price("btc.txt")
File "getICOdate.py", line 155, in initial_price
coin_dates = [d["Date"] for d in f]
File "getICOdate.py", line 155, in <listcomp>
coin_dates = [d["Date"] for d in f]
TypeError: string indices must be integers
I want to open the a txt files and save the dates and prices into two separate lists and I'm not sure how to approach it