【发布时间】:2019-07-08 09:50:29
【问题描述】:
我在一个文件中有一个字典,并从文件中打印名称值
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'}]}
#读取文件
import os
with open('g.txt','r') as fh:
fh_n = fh.read()
#保存到列表中
test = []
for k,v in di.items():
test.append(v[0]['name'])
test
['猫','鼠标']
【问题讨论】:
-
文件中的字典 - 试图从另一个 py 文件中解析
.py文件?详细说明您的问题 -
如果您在 a.py 中有一个 dict 并且您想在 b.py 中使用它,只需使用 import - 无需打开文件。
-
你应该考虑使用.json文件而不是文本文件,看看developer.rhino3d.com/guides/rhinopython/python-xml-json。
标签: python file dictionary