python3 查找文件_python3从文本文件中查找信息?

在我写的一个程序中,我每天将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文件并将日期和价格保存到两个单独的列表中,我不知道如何处理它

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值