0

I am a python newbie; it is highly likely someone has asked a similar question, though I can't find one. When I query for objects from a model created in another program I get a list like this:

lineObj = [obj for obj in model.objects if obj.type == OF.otLine]

print lineObj
[<Line: 'Upper1'>, <Line: 'Upper2'>, <Line: 'MooringLine'>, <Line: 'BuoyLine'>, <Line: 'RiserLine1'>, <Line: 'RiserLine2'>, <Line: 'AnchorLine1'>, <Line: 'AnchorLine2'>, <Line: 'AnchorLine3'>]

print lineObj[0] 
<Line: 'Upper1'>

I would like to be able to pull only the text Upper1, Upper2, etc., but I am not sure how. It may be better to use a dictionary or something else. Thanks in advance for any feedback.

aSea
  • 91
  • 2
  • 3
  • 9
  • Have you tried `print str(lineObj[0])` ? – Anand S Kumar Jul 01 '15 at 16:32
  • yes, this produces: – aSea Jul 01 '15 at 16:36
  • by "model created in another program" is the other program a commonly used library (and if so, which one) or not? Alternatively, what does `dir(lineObj[0])` say? – NightShadeQueen Jul 01 '15 at 16:36
  • `` is what is output by `OF.otline`'s `__str__` function. If you know what the property is, you can print that out. `print(lineObj[0].name)` etc. – Brobin Jul 01 '15 at 16:37
  • 1
    Just so you understand, the reason you're getting more than just the main text is because it's a list of objects, not strings. So in order to print just that name you'll need to access it from the object. If you try putting in `dir(lineObj[0])` you'll get a list of method names, and one of them should provide you with the name you want. – SuperBiasedMan Jul 01 '15 at 16:37
  • it is an orcaflex file, and an orcaflex api is used to access the resulting data. there is a long laundry list from dir(lineObj[0]) – aSea Jul 01 '15 at 16:39
  • `obj` should be `obj.xxx` so that you can get strings like 'Upper1' – LittleQ Jul 01 '15 at 16:43
  • thanks @Brobin, print(lineObj[0].name) works great! – aSea Jul 01 '15 at 16:45
  • @aSea Awesome! I put it into an answer below. If you could accept it, that would be greatly appreciated. – Brobin Jul 01 '15 at 17:30

3 Answers3

0

If print str(lineObj[0]) produces the string <Line: 'Upper1'> as listed in the comments, this should work to clean up your list:

lineObj = [str(item).split("'")[1] for item in lineObj]

print lineObj[0] now produces Upper1

laurak
  • 363
  • 3
  • 9
0

You are getting <Line: 'Upper1'> because that is how the object's __str__ function is formatted.

The best way to get the result you want would be to figure out what attribute of the object you are looking for. In this case, that is name.

lineObj = [obj.name for obj in model.objects if obj.type == OF.otLine]

print lineObj
>>> ['Upper1', 'Upper2', 'MooringLine', 'BuoyLine', 'RiserLine1', 'RiserLine2', 'AnchorLine1', 'AnchorLine2', 'AnchorLine3']

print lineObj[0]
>>> 'Upper1'
Brobin
  • 3,241
  • 2
  • 19
  • 35
0

I'm guessing you are using OrcaFlex and your actual problem is getting the names of the Line objects.

A relatively recent version of the pyoxf API will contain some helpers for retrieving the lines and the example in the doc indicates the name is available as the "name" property:

lineNames = [lineObject.name for lineObject in model.lines]
Mikko Virkkilä
  • 176
  • 1
  • 4