Parsing string list in python -


i trying remove comments when printing list.

i using

output = self.cluster.execcmdverify('cat /opt/tpd/node_test/unit_test_list') item in output:     print item 

this perfect giving me entire file, how remove comments when printing?

i have use cat getting file due located.

the function self.cluster.execcmdverify returns iterable, can this:

import re  def remove_comments(line):     """return empty string if line begins #."""     return re.sub(re.compile("#.*?\n" ) ,"" ,line)     return line  data = self.cluster.execcmdverify('cat /opt/tpd/node_test/unit_test_list')  line in data:     print remove_comments(line) 

the following example string output:

to flexible, can create file-like object string (as far string)

from cstringio import stringio import re  def remove_comments(line):     """return empty string if line begins #."""     return re.sub(re.compile("#.*?\n" ) ,"" ,line)     return line  data = self.cluster.execcmdverify('cat /opt/tpd/node_test/unit_test_list') data_file = stringio(data)  while true:     line = data_file.read()     print remove_comments(line)     if len(line) == 0:         break 

or use remove_comments() in for-loop.


Comments