Ask Your Question
1

Selecting data to send to Excel - set_range

asked 2016-06-27 18:19:59 -0500

SAE_2016 gravatar image

Hi! I’m newbie with python and I try to export selected bus data to Excel. I made this code base on this video from Jervis Whitley

# import os,sys
PythonPath = r'C:\Program Files\PTI\PSSE32\PSSBIN'
sys.path.append(PythonPath)
os.environ['PATH'] = (r"C:\Program Files\PTI\PSSE32\PSSBIN;"
                      + os.environ['PATH'])
import excelpy
import psspy
import redirect
redirect.psse2py()
psspy.psseinit(10000)
psspy.case(r'savnw.sav')
# Buses in service 
ierr, nbuses = psspy.abuscount(-1, 1)
# Read data from sav...
ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')
# Open and Show excel
x1 = excelpy.workbook()
x1.show()
# Write Titel on Excel
x1.set_cell('a1', 'AREA')
x1.set_cell('b1', 'Bus Number')
x1.set_cell('c1', 'Voltage [pu]')
# Send data to Excel
x1.set_range(2,'a',zip(*iarea))
x1.set_range(2,'b',zip(*buses))
x1.set_range(2,'c',zip(*volts))
# Save Workbook
x1.save('demo.xlsx')

Thas work fine but when I try to send for example ONLY the data from Area 5 I can’t!. It’s a possibility in Pyhton to do a of this data? A code like:

for jj in range (0,nbuses):
    if iarea[jj]==5:
        x1.set_range(2,'a',zip(*iarea(jj)))
        x1.set_range(2,'b',zip(*buses(jj)))
        x1.set_range(2,'c',zip(*volts(jj)))

doesn’t work. Thanks a lot in advance!

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
1

answered 2016-07-03 11:31:37 -0500

jconto gravatar image

updated 2017-08-27 17:40:46 -0500

In this version A5buses.py, I avoid the use of zip + enumerate and recognizing the format of the orginal arrays, select corresponding items valid for area 5 only.

Download it from my Google_drive. Edit 'PythonPath' within the code to fit your system.

edit flag offensive delete link more

Comments

Excellent! Also, I dont like also very much "enumerate". Thank you JConto, works all ok.

SAE_2016 gravatar imageSAE_2016 ( 2016-07-04 18:28:41 -0500 )edit

I want to know how to use this method on psse33 & python2.7 in this video

YI-WEI gravatar imageYI-WEI ( 2017-08-27 12:01:43 -0500 )edit
0

answered 2016-06-27 21:28:19 -0500

JervisW gravatar image

updated 2016-06-27 21:29:21 -0500

At this point here, you have all the information you want, but spread in three different lists (or columns). buses, iarea and volts

ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')

What you want is a way to filter for the rows where the area == 5

You need to transpose your columns into rows first.

rows = zip(buses, iarea, volts)

then you'll need to set the cells that you'd like. I don't think excelpy is efficiently using set_range. It is just calling set_cell individually. So we can do this without any performance penalty

for rowid, record in enumerate(rows):
    bus, area, voltage = record
    if area == 5:
        x1.set_cell(2 + rowid, 'a', area)
        x1.set_cell(2 + rowid, 'b', bus)
        x1.set_cell(2 + rowid, 'c', voltage)

where enumerate obviously gives us a 0-based index, and you wanted to start from row 2, that's why I add 2 to every rowid.

edit flag offensive delete link more
0

answered 2016-06-29 19:48:49 -0500

SAE_2016 gravatar image

Hi Jervis!, Thank you very much for your answer. Unfortunately the script don't send any data to the Workbook. Python continuous still hard to me and I guess I made a fool mistake but "hello 5!" doesn't appear nowhere :(

# Read ...
ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')
# Open and Show excel
x1 = excelpy.workbook()
x1.show()
# Write Titel on Excel
x1.set_cell('a1', 'AREA')
x1.set_cell('b1', 'Bus Number')
x1.set_cell('c1', 'Voltage [pu]')
# Transpose the columns into rows 
rows = zip(buses, iarea, volts)
# Send data to Excel
for rowid, record in enumerate(rows):
    bus, area, voltage = record    
    print 'hello all!'
    if area == 5:
        print 'hello 5!'
        x1.set_range(2 + rowid, 'a', area)
        x1.set_range(2 + rowid, 'b', bus)
        x1.set_range(2 + rowid, 'c', voltage)
# Save Workbook
x1.save('demo.xlsx')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

2 followers

Stats

Asked: 2016-06-27 18:19:59 -0500

Seen: 1,259 times

Last updated: Aug 27 '17