0

I am writing a python script to automate some simulations with Aspen Plus via its COM functions. But when I want to get access to molecular weights values, I have to write something like this:

import os
import win32com.client as win32

aspen = win32.Dispatch('Apwn.Document')

aspen.InitFromArchive2(os.path.abspath('Aspen\\Flash.bkp'))

MW = aspen.Tree.Data.Properties.Parameters.Pure Components.REVIEW-1.Input.VALUE.MW ACID.Value

But it launchs a syntax error in REVIEW-1, due to hyphens can not be used as identifiers. How can I use them like that?

EDIT:

I replaced dot synax for FindNode function of Aspen COM like that:

MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW-1")

But I still get a None object, however this:

MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components")

works, getting the "COMObject FindNode" so I think that the problem is in the hyphen as well.

Thanks in advance!

5
  • 1
    prior to that there are spaces as well. using dashes and spaces in identifiers are just not valid python Commented Jul 30, 2018 at 8:21
  • Have you tried using importlib? docs.python.org/3/library/… Commented Jul 30, 2018 at 8:28
  • I'll take a look to importlib, but could it be used with COM functions?? Also, I updated my question with an edit if someone find an answer by this way Commented Jul 30, 2018 at 8:45
  • can you list the available sub-nodes from the valid non-None node you're getting? I'm sure it's just a casing/hyphen type issue now Commented Jul 30, 2018 at 8:46
  • [Maybe an image is more helpful][1] [1]: i.sstatic.net/4AZtJ.png Commented Jul 30, 2018 at 9:41

2 Answers 2

1

Thanks for the tip

For cases of having hyphens, the following should work instead of escaping the "\" character:

MW = aspen.Tree.FindNode(r'\Data\Properties\Parameters\Pure Components\REVIEW-1\Input\VALUE')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ejay! By the way, I am working with another simulation now and I don't have any problems with spaces or hyphens. So I don't understand anything!
0

Ok, I was breaking my head trying to solve it in Python, but was easier solving it in Aspen renaming the node. I've noticed, also, that spaces sometimes give problems too, so should be renamed as well. In some cases it can't be done or I don't know how, for example:

MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW1\\Input\\VALUE\\MW ACID")

It returns a None object and I don't know hot to rename "MW ACID", but there is a tricky way to get the value:

MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW1\\Input\\VALUE")

for o in MW.Elements:
    if o.Name == "MW ACID":
        MW_acid = o.Value

For now it works for me, however it will be slower due to iteration. So if someone knows how to solve the problem in Python without renaming names, it will be still helpful. I tried to use unicode and bytes notation for non-breaking hyphen but it didn't works too.

Regards!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.