2

I am trying to remove the comments when printing this list.

I am using

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

This is perfect for giving me the entire file, but how would I remove the comments when printing?

I have to use cat for getting the file due to where it is located.

0

4 Answers 4

2

The function self.cluster.execCmdVerify obviously returns an iterable, so you can simply do this:

import re

def remove_comments(line):
    """Return empty string if line begins with #."""
    return re.sub(re.compile("#.*?\n" ) ,"" ,line)
    return line

data = self.cluster.execCmdVerify('cat /opt/tpd/node_test/unit_test_list')

for line in data:
    print remove_comments(line)

The following example is for a string output:

To be flexible, you can create a file-like object from the a string (as far as it is a string)

from cStringIO import StringIO
import re

def remove_comments(line):
    """Return empty string if line begins with #."""
    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 just use remove_comments() in your for-loop.

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work well due to our testing framework and our systems are clustered with several nodes, so I cannot rlogin to the correct node within Python.
I used this buy I got this:
This error was obtained, any ideas? data_file = StringIO(data) TypeError: expected read buffer, tuple found
data is obviously not a string, so just do it without StringIO with a for-loop. I'll update my post…
2

You can use regex re module to identify comments and then remove them or ignore them in your script.

Comments

0

What about greping the output

grep -v '#' /opt/tpd/node_test/unit_test_list

Comments

0

If it's for a python file for example and you want to remove lines beginning with # you can try :

cat yourfile | grep -v '#'

EDIT:

if you don't need cat, you can directly do :

grep -v "#" yourfile

3 Comments

it's just for a text file located on a server.
why using cat when you can use grep directly?, I guess when Alan says he needs to use cat, he means that he can't use python to open the file directly, but can use other commands as far as I understand
Oh ok, I thought you absolutely had to use the cat command, this will still work though, when I said python file it was for the example of removing lines with #, it will still work with a textfile, just replace # by your comment syntax. But it would be best to use grep directly like egrep -v "#" yourfile.

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.