尝试动态创建函数/将其与for循环中的变量关联

问题描述

我的代码从csv读取参数以创建对象(在这种情况下,总线是电力系统中的公共元素),并将每个对象与变量关联。我正在尝试使用for循环在csv文件中创建与行数一样多的总线(列是每个总线的参数)的总线,我认为我已经实现了创建变量bus0 bus1 bus2等等的目的并且具有在csv中读取的参数的数据帧也在那里并且可以访问。

但是当我尝试使用此类变量(bus0,bus1 ...)来执行其他操作(例如,将生成点附加到bus0)时,即使我看到了“ bus0不存在”,它仍然会说“ bus0不存在”可变窗口。我尝试了很多事情,但看起来不起作用,非常感谢您的帮助。我在下面附上我的代码片段:

    import pandapower as pp
    import csv

    net = pp.create_empty_network() #create an empty network


    with open('Bus_data_test.csv','r') as Bus_data:
         BD = [row for row in csv.reader(Bus_data)]
    print(BD)

    Buses={}

    i=0
    for rows in BD:

        Buses["Bus{0}".format(i)]= pp.create_bus (net,vn_kv=float(BD[i][0]),name=BD[i][1],index=int(i),geodata=None,type=str(BD[i][4]),zone=int(BD[i][5]),in_service=bool(BD[i][6]),max_vm_pu=float(BD[i][7]),min_vm_pu=float(BD[i][8]),coords=BD[i][9])
i=i+1
   print(net.bus)
   print(net.bus.iat[0,5])

    with open('Gen_data.csv','r') as Gen_data:
        GD = [row for row in csv.reader(Gen_data)]

    print(GD)  

    Generators={}

     i=0
    for rows in GD:

       Generators["Gen{0}".format(i)] = pp.create_gen(net,GD[i][0],p_mw=GD[i][1],vm_pu=GD[i][2],name=GD[i][3],max_q_mvar=GD[i][7],min_q_mvar=GD[i][8],min_p_mw=GD[i][6],max_p_mw=GD[i][5],scaling=GD[i][9],type=str(GD[i][10]),slack=bool(GD[i][11]),controllable=GD[i][12],in_service=GD[i][13])
       i=i+1
    print(net.gen)

这是csv的片段:

Bus0,230,Bus0,None,b,1,True,1.1,0.9,bus0,50,30.99,1.0,True
Bus1,Bus1,bus1,170,105.35,True

回溯错误

C:\Users...lib\site-packages\pandapower\create.py:1177: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison
  if bus not in net["bus"].index.values:
Traceback (most recent call last):

  File "<ipython-input-11-e022521b4210>",line 1,in <module>
    runfile('C:/Users...Net_reader_test.py',wdir='C:/Users/...Matpower')

  File "C:\Users\...lib\site-packages\spyder_kernels\customize\spydercustomize.py",line 827,in runfile
    execfile(filename,namespace)

  File "C:\Users\...\lib\site-packages\spyder_kernels\customize\spydercustomize.py",line 110,in execfile
    exec(compile(f.read(),filename,'exec'),namespace)

  File "C:/Users/...Net_reader_test.py",line 41,in <module>
    Generators["Gen{0}".format(i)] = pp.create_gen(net,in_service=GD[i][13])

  File "C:\Users\...\lib\site-packages\pandapower\create.py",line 1178,in create_gen
    raise UserWarning("Cannot attach to bus %s,bus does not exist" % bus)

UserWarning: Cannot attach to bus Bus0,bus does not exist

解决方法

您可以创建以Buses中的键命名的变量。这不是常见的事情,但这是一个示例。我对Buses的创建进行了一些整理,而忽略了其他代码,只专注于解决这个问题。

import pandapower as pp
import csv

net = pp.create_empty_network() #create an empty network

Buses = {}
with open('Bus_data_test.csv','r') as Bus_data:
    for index,row in enumerate(csv.reader(Bus_data)):
        Buses["Bus{0}".format(index)] = pp.create_bus(net,vn_kv=float(row[0]),name=row[1],index=index,geodata=None,type=row[4],zone=int(row[5]),in_service=(row[6]=="True"),max_vm_pu=float(row[7]),min_vm_pu=float(row[8]),coords=row[9])

assert len(Buses) > 0

# normally one would just use the name in the dict
print(Buses["Bus0"])

# but module level variables can be constructed
for name,value in Buses.items():
    globals()[name.lower()] = value
print(bus0)