在我写的一个程序中,我每天将BTC的平均价格保存到.txt文件中,格式如下: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
...
在我的代码中,我有一个日期列表,我必须将列表中的日期与文本文件中的日期进行匹配,然后查找当天的平均BTC价格。我打算将所有日期和价格分别保存在一个名为“coin_dates”和“coin_prices”的列表中。
我试图在典型的json字典打开之后对代码建模,区别在于我拿走了“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")
但我得到这个错误:Traceback (most recent call last):
File "getICOdate.py", line 158, in
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
coin_dates = [d["Date"] for d in f]
TypeError: string indices must be integers
我想打开一个txt文件并将日期和价格保存到两个单独的列表中,我不知道如何处理它