【问题标题】:taking a list of dictionaries as input from a file将字典列表作为文件的输入
【发布时间】:2016-04-16 08:01:46
【问题描述】:

我想从名为 IP.txt 的文件中获取以下字典列表作为输入,并将其存储在名为 lst 的变量中:

[{"date":"30-Apr-12","price":583.98},{"date":"1-May-12","price":58}]

我该怎么做?

【问题讨论】:

  • 看看json 模块。
  • 如果您使用json 模块,这很简单。

标签: python list file dictionary


【解决方案1】:
import json    
with open("IP.txt") as json_file:
        Ist= json.load(json_file)
        print(Ist)

>>>[{'price': 583.98, 'date': '30-Apr-12'}, {'price': 58, 'date': '1-May-12'}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多