我正在编写一个脚本,可以将.json格式转换为.idf格式,以便在EnergyPlus中进行能量模拟。作为这个脚本的一部分,我需要根据一些时间点和值来创建时间表,直到给定的时间为止。例如,这是我试图转换的.json元素中的一个:
"BOT": {"SpacesInModel": [
{"IndoorClimateZone": {
"Schedules": {
"PeopleSchedule": {"Timer": [
{ "$numberInt": "0" },
{ "$numberInt": "10" },
{ "$numberInt": "20" },
{ "$numberInt": "24" }
],
"Load": [{ "$numberDouble": "0.5" }, { "$numberInt": "1" }, { "$numberDouble": "0.5" }]
}目前,我已经创建了以下代码,用于读取和写入.idf文件所需的输入,但我希望将其作为for循环而不是若干if语句来执行。
### Define helper function determining the number format
def IntOrDouble(path_string):
try:
return_value = path_string["$numberInt"]
except Exception:
return_value = path_string["$numberDouble"]
return(return_value)
### Create .idf object and loop over .json format extracting schedule inputs
for item in data['BOT']['SpacesInModel']:
InputFile.newidfobject("Schedule:Day:Interval") #.idf object
DailySchedule = InputFile.idfobjects["Schedule:Day:Interval"][-1]
People_Time = item["IndoorClimateZone"]["Schedules"]["PeopleSchedule"]["Timer"]
People_Load = item["IndoorClimateZone"]["Schedules"]["PeopleSchedule"]["Load"]
if len(People_Time) >= 0 and len(People_Time) != 0:
DailySchedule.Time_2 = IntOrDouble(People_Time[0])
if len(People_Time) >= 1 and len(People_Time) != 1:
DailySchedule.Time_2 = IntOrDouble(People_Time[1])
DailySchedule.Value_Until_Time_2 = IntOrDouble(People_Load[0])
if len(People_Time) >= 2 and len(People_Time) != 2:
DailySchedule.Time_3 = IntOrDouble(People_Time[2])
DailySchedule.Value_Until_Time_3 = IntOrDouble(People_Load[1])
if len(People_Time) >= 3 and len(People_Time) != 3:
DailySchedule.Time_4 = IntOrDouble(People_Time[3])
DailySchedule.Value_Until_Time_4 = IntOrDouble(People_Load[2])
if len(People_Time) >= 4 and len(People_Time) != 4:
DailySchedule.Time_5 = IntOrDouble(People_Time[4])
DailySchedule.Value_Until_Time_4 = IntOrDouble(People_Load[3])我的问题是,我不知道如何将变量DailySchedule与可变对象名/path (例如Time_1或Load_4 )“连接”。Time_i Load_i的索引必须遵循for循环的索引。到目前为止,这是我得到的最接近的(知道这不是一个真正的解决方案:-)
for i in range(len(People_Time)):
DailySchedule."Time_{0}".format(i+1) = IntOrDouble(People_Time[i+1])
DailySchedule."Load_{0}".format(i+1) = IntOrDouble(People_Load[i]) https://stackoverflow.com/questions/70710866
复制相似问题