سؤال

I have a output from a command which looks like this :

asdf> show status
Ok
Will be patched
fgh>
Need this
>

What I want to do is strip the output of every line which contains ">" and store the output in a variable(result),such that on issuing print result ,I get:

Ok
Will be patched
Need this

This is what I currently have :

offending = [">"]
#stdout has the sample text
for line in stdout.readlines():
    if not True in [item in line for item in offending]:
        print line

Currently it just prints the line.I instead want it to be stored in a variable such that printing the variable prints the entire output i desire.

EDIT : For more clarity on what I am doing,results from the command line interpreter:

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>> import paramiko
>>> offending = [">"]
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> conn=ssh.connect('10.12.23.34', username='admin', password='admin', timeout=4)
>>> stdin, stdout, stderr = ssh.exec_command('show version')
>>> print stdout.read()

bcpxyrch1>show version
Version: SGOS 6.2.12.1 Proxy Edition
Release id: 104304
UI Version: 6.2.12.1 Build: 104304
Serial number: 3911140082
NIC 0 MAC: 00D083064C67
bcpxyrch1>

>>> result = '\n'.join(item for item in stdout if offending not in item)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
TypeError: 'in <string>' requires string as left operand, not list
>>>
هل كانت مفيدة؟

المحلول

result = '\n'.join(item for item in stdout.read().splitlines() if '>' not in item)

That is what you need to do. When you print(result), it will output exactly as specified in your question.

نصائح أخرى

I think it's more readable to use filter in this case.

filter(lambda s: '>' not in s, stdout.read().splitlines())
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top