import ast
with open('g.txt','r') as fh:
fh_n = fh.read()
#first split string and convert into dictionary
data = ast.literal_eval(fh_n.split("=")[1].strip())
#or
#di = remove from text file
#ast.literal_eval(fh_n)
name = [i[0]['name'] for i in data.values()]
print(name)
O / P:
['cat', 'mouse']
ИЛИ
преобразовать данные текстового файла в файл json
g.json
файл
[{
"di": {
"elk": [
{
"url_1": "localhost:8080/api/running",
"url_2": "localhost:8080/api/",
"name": "cat",
"method": "GET"
}
],
"a": [
{
"url_1": "localhost:8080/api/running",
"url_2": "localhost:8080/api/",
"name": "mouse",
"method": "GET"
}
]
}
}
]
.py
file
import json
with open('g.json') as fh:
data = json.load(fh)
name = [i[0]['name'] for i in data[0]['di'].values()]
print(name)
O / P:
['cat', 'mouse']