【发布时间】:2018-06-14 00:25:45
【问题描述】:
在我编写的一个程序中,我将每天 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 <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
我想打开一个 txt 文件并将日期和价格保存到两个单独的列表中,但我不知道如何处理它
【问题讨论】:
标签: python-3.x pandas text-files