【发布时间】:2014-09-21 07:01:36
【问题描述】:
我想从字典中查询项目并将打印输出保存到文本文件中。
这是我所拥有的:
import json
import exec.fullog as e
inp = e.getdata() #inp now is a dict() which has items, keys and values.
#Query
print('Data collected on:', inp['header']['timestamp'].date())
print('\n CLASS 1 INFO\n')
for item in inp['Demographics']:
if item['name'] in ['Carly', 'Jane']:
print(item['name'], 'Height:', item['ht'], 'Age:', item['years'])
for item in inp['Activity']:
if item['name'] in ['Cycle', 'Run', 'Swim']:
print(item['name'], 'Athlete:', item['athl_name'], 'Age:', item['years'])
【问题讨论】:
-
简单地说,继续打印任何你想要的东西,到控制台,就像你在 Python 文件中使用
print或log一样;这里没有具体的变化。现在,在执行python文件python3 yo.py时,只需将命令替换为python3 yo.py > logs.txt
标签: python python-2.7