python程序,用于存储文件中的特定字符串并将其存储在变量中

时间:2018-01-23 07:36:43

标签: python python-3.x python-2.7

我想存储ex: -

文件中的特定字符串

2 个答案:

答案 0 :(得分:0)

在我看来,你的文字格式是json。在这种情况下答案很简单:

import json

with open('file.txt') as f:
    data = json.loads(f.read())
    # access the required key
    my_variable = data['my_id']

答案 1 :(得分:0)

首先关闭

在您的示例json文件中,您使用引号不一致,选择'或"并坚持下去。

代码

import json
data = json.load(open("file.json"))
# this is the variable you are after
variable = data["my_id"]