[Python] How to print the elements in a double list in this code?

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I want to print the elements in the buses_lines double list. The point of the code is to take in a BusData.txt file and store the elements in a list.

    This is the data file

    # Bus Data
    # BusID, Type, VoltageMag, VoltageAng, ActivePower, ReactivePower
    1, Gen, 1.0, 0.0, 1.0, 0.0
    2, Load, 0.0, 0.0, 0.6, 0.3
    3, Gen, 1.0, 0.0, 0.0, 0.0
    4, Load, 0.0, 0.0, 0.6, 0.2
    5, Load, 0.0, 0.0, 0.5, 0.4

    # Line Data
    # Line, StartBus, EndBus, ImpedanceR, ImpedanceX, Capacitance
    1, 1, 2, 0.05, 0.25, 0.05
    2, 2, 3, 0.05, 0.25, 0.05
    3, 1, 4, 0.02, 0.2, 0.033333
    4, 2, 4, 0.02, 0.2, 0.033333
    5, 4, 5, 0.01, 0.1, 0.02


    class Bus:
    idx:int
    u:complex
    i_inj: complex
    bus_type: int #define 0 slack, 1 pv, 2 pq
    #y: complex # ?
    p: int
    q: int
    q_min: int
    q_max: int


    def __init__(self, idx, bus_type, voltage_mag, voltage_ang, p, q, y, q_min, q_max):
    self.idx = idx
    self.bus_type = bus_type
    self.u=complex(voltage_mag*np.cos(voltage_ang),voltage_mag*np.sin(voltage_ang))
    self.voltage_mag = voltage_mag
    self.voltage_ang = voltage_ang
    self.p = p
    self.q = q
    self.y=y # maybe
    self.q_min = q_min
    self.q_max = q_max

    def set_to_pv(self, voltage_mag, active_power):
    self.voltage_mag = voltage_mag
    self.bus_type = 1
    self.active_power = active_power
    print("Bus switched to PV")


    def set_to_pq(self, active_power, reactive_power):
    self.bus_type = 2
    self.active_power = active_power
    self.reactive_power = reactive_power
    print("Bus switched to PQ")

    def switch_pq (self):
    if self.q < self.q_min:
    self.q = self.q_min
    self.set_to_pq(self.p, self.q)
    print("Reactive power below limits")
    elif self.q > self.q_max:
    self.q = self.q_max
    self.set_to_pq((self.p, self.q))
    print("Reactive power above limits")

    def switch_back(self):
    if self.q_min <= self.q <= self.q_max:
    if self.bus_type == 2:
    self.set_to_pv(self.voltage_mag, self.active_power)
    print("Bus switched to PV")

    class Line:
    start_bus: int
    end_bus: int
    z: complex
    y:complex

    def __init__(self, line_id, start_bus, end_bus, r, x, capacitance):
    self.line_id = line_id # what is this?
    self.start_bus = start_bus
    self.end_bus = end_bus
    self.z = complex(r, x)
    try:
    self.y=1/self.z
    except ZeroDivisionError as err:
    self.y=0
    print(err, 'at line', start_bus, end_bus)
    self.capacitance = capacitance
    def read_file(filename):
    buses = []
    lines = []
    section = None
    with open(filename, "BusData.txt") as file:
    reader = csv.reader(file)
    for row in reader:
    if "# Bus Data" in row:
    section = "bus"
    next(reader) # Skip header
    elif "# Line Data" in row:
    section = "line"
    next(reader) # Skip header
    elif section == "bus":
    bus = Bus(
    idx=int(row[0]),
    bus_type=row[1],
    voltage_mag=float(row[2]),
    voltage_ang=float(row[3]),
    p=float(row[4]),
    q=float(row[5])
    #z=complex(row[6],row[7])
    )
    buses.append(bus)
    elif section == "line":
    line = Line(
    line_id=int(row[0]),
    start_bus=int(row[1]),
    end_bus=int(row[2]),
    r=float(row[3]),
    x=float(row[4]),
    capacitance=float(row[5])
    )
    lines.append(line)
    buses_lines=[buses,lines]

    return buses_lines



    I've tried this:

    f = open("BusData.txt", "r")
    read = f.read()
    rf = read_file(read)
    print(rf[0][1].idx)


    I get traceback error or rf, and invalid mode "BusData.txt". Update: Now i get that it wants me to add q_min and q_max to the read file function. The thing is i don't want to read that from file and add to a bus, as it is specified limits and should only be defined.

    Continue reading...

Compartilhe esta Página