首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在for循环中动态创建函数/将函数与变量相关联

尝试在for循环中动态创建函数/将函数与变量相关联
EN

Stack Overflow用户
提问于 2020-08-12 02:22:37
回答 1查看 90关注 0票数 1

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

但是当我尝试使用这样的变量(bus0,bus1...)为了做其他事情(比如给bus0附加一个生成点),它一直说"bus0不存在“,尽管我在我的变量窗口中看到了它。我尝试了很多东西,但看起来不起作用,感谢任何帮助,谢谢。我附加了下面的代码片段:

代码语言:javascript
复制
    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], index=int(i), 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的一个片段:

代码语言:javascript
复制
Bus0,230,Bus0,0,None,b,1,True,1.1,0.9,None,bus0,50,30.99,0,0,None,1.0,0,True
Bus1,230,Bus1,1,None,b,1,True,1.1,0.9,None,bus1,170,105.35,0,0,None,1.0,1,True

回溯错误:

代码语言:javascript
复制
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, GD[i][0], p_mw=GD[i][1], vm_pu=GD[i][2], name=GD[i][3], index=int(i), 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])

  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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-12 03:09:29

您可以在Buses中创建以键命名的变量。这不是一件常见的事情,但这里有一个示例。我清理了一些Buses的创建,并省略了其他代码,以便只关注这个问题。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63364203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档