1

For a project I have to extract the rgb data in a file which is defined as following:

#98=IFCCOLOURRGB($,0.26,0.22,0.18);

I've been using regex and with help from here I came up with this;

IfcColourData = re.compile("IFCCOLOURRGB\(\$,([\d\.]+),([\d\.]+),([\d\.]+)")

It outputs:

('0.26', '0.22', '0.18')

Now how do I get rid of the parentheses and apostrophes when writing to a file or printing to the console?

I want to output it like this:

0.26 0.22 0.18

Edit:

This is the code:

import re 

IfcFile = open('IfcOpenHouse.ifc', 'r')

IfcColourData = re.compile("IFCCOLOURRGB\(\$,([\d\.]+),([\d\.]+),([\d\.]+)")

RadFile = open('IFC2RAD.rad', 'w')

for RadColourData in IfcFile:
    RGB_Data = IfcColourData.search(RadColourData)
    if RGB_Data:
        print(RGB_Data.groups())
        RadFile.write('mod material name' + '\n')
        RadFile.write('0' + '\n')
        RadFile.write('0' + '\n')
        RadFile.write(str(RGB_Data.groups()) + '\n' + '\n')  


#Closing IFC and RAD files  
IfcFile.close() 
RadFile.close()
Claus
  • 119
  • 1
  • 12

1 Answers1

0

That output is a tuple, with three elements. You can get at the elements of a tuple t with the indices of the items:

print t[0], t[1], t[2]

You could also do a little loop:

>>> import re
>>> s = "#98=IFCCOLOURRGB($,0.26,0.22,0.18);"
>>> c = re.compile("IFCCOLOURRGB\(\$,([\d\.]+),([\d\.]+),([\d\.]+)")
>>> r = re.search(s)
>>> g = r.groups()
>>> g
('0.26', '0.22', '0.18')
>>> print g[0], g[1], g[2]
0.26 0.22 0.18
>>> for e in g: print e,
... 
0.26 0.22 0.18

Bottom line: if you change your relevant line to this, it will work:

g = RGB_Data.groups()
RadFile.write('{0} {1} {2}\n\n'.format(g[0], g[1], g[2])) 
Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • [`string.format()`](https://docs.python.org/2/library/string.html#formatspec) is just a convenient way of substituting text (e.g. from a variable) into a string. [This thread](http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) might help. It's really useful for making strings out of variables. – Matt Hall Jan 11 '15 at 20:30