【发布时间】:2019-04-24 06:34:00
【问题描述】:
这是json文件
{"pre_trigger": 4, "sampling frequency": 1652, "record length": 15.0,
"sensors":
[{"model": "393B05", "serial": "46978", "sensitivity": 10030, "sensitivity_units": "mV/g", "sensor_type": "Accelerometer", "units": "g", "location": [7.01, -0.19, 0], "location_units": "m", "direction": [0, 0, 1], "trigger": true, "trigger_value": 0.005, "max_val": 0.45, "min_val": -0.45, "comments": "Inside B122 next to bookshelf", "channel": "cDAQ1Mod2/ai0"}],
[{"model": "393B05", "serial": "47085", "sensitivity": 9980, "sensitivity_units": "mV/g", "sensor_type": "Accelerometer", "units": "g", "location": [9.65, -0.19, 0], "location_units": "m", "direction": [0, 0, 1], "trigger": true, "trigger_value": 0.005, "max_val": 0.45, "min_val": -0.45, "comments": "Inside B122 under the whiteboard", "channel": "cDAQ1Mod2/ai1"}]
"parameters": {"general": [], "specific": ["Walking direction", "Person ID"]}}
我不是一个懂编码的人,所以我不知道这个错误真正来自哪里。我正在运行以下命令
daq = DAQ()
daq.load_setup('json.fname')
返回属性错误。 json文件中没有单引号,所以我真的不知道问题出在哪里。下面是错误回调的地方。
def load_setup(self,fname='setup.json'):
"""
Opens the JSON file containing the setup parameters for the experiment.
Parameters
----------
fname : str
File that the parameters for the experiment were saved into (JSON file)
"""
import json
with open(fname, 'r') as setup_file:
setup_data = json.load(setup_file)
self.fs = setup_data['sampling frequency']
self.record_length = setup_data['record length']
self.sensors = setup_data['sensors']
self.parameters = setup_data['parameters']
self.pre_trigger = setup_data['pre_trigger']
【问题讨论】:
-
没有足够的信息,因此我们可以尝试您的代码案例并了解问题。您能否添加更多代码,例如类的代码,并更好地解释您尝试通过这两行实现的目标。但是,让我猜猜,如果您使用传递的字符串
exec()执行函数,我认为您正在访问 json 对象上没有的属性。 -
是的,我会尝试的。我将在错误回调的位置添加到上面。