0

I need to compare two string in python, first string is read from .xlsx file and second is an output from stdout.readlines().

Below code is to get command output.

stdin, stdout, stderr = client.exec_command(testCommand)
op = stdout.readlines()
print("op =\n"+str(op))
str1 = "".join(op)

Since some commands output begin with \t or might have \t in between .

For Eg : Below command output begin with \t and after LEN there is \t.

#   PASS_MIN_LEN    Minimum acceptable password length.
PASS_MIN_LEN    5

And xlsx file is having

# PASS_MIN_LEN Minimum acceptable password length.
PASS_MIN_LEN 5

As .xlsx comparison string doesn't have \t, how can i ignore \t while comparing two string.

if cmdOutput== xlsxOutput:

is not working.

I tried to trim the cmdOutput with \t, it didn't worked. Any approach can i follow?

7
  • Is op a list? Commented Aug 28, 2017 at 18:52
  • @cᴏʟᴅsᴘᴇᴇᴅ I'm 99.999999999% sure it is. stdout looks like a file, and file.readlines() returns a list. Not to mention the OP's use of str.join. Commented Aug 28, 2017 at 18:53
  • @ChristianDean Mhm... thought so... I suppose OP tried something like str(op).strip(). Commented Aug 28, 2017 at 18:54
  • Its is a list. [u'#\tPASS_MIN_LEN\tMinimum acceptable password length.\n', u'PASS_MIN_LEN\t5\n' ] Commented Aug 28, 2017 at 18:54
  • 1
    @Sameer So you want to replace all tabs with one space? Commented Aug 28, 2017 at 18:55

3 Answers 3

1

if you just want to replace tabs with a space, perhaps str.replace is simple enough. But that doesn't leave the trailing newlines. You might consider the replacement followed by str.strip. For example:

op = [x.replace('\t', ' ').strip() for x in op]
print(op)

['# PASS_MIN_LEN Minimum acceptable password length.', 'PASS_MIN_LEN 5']

If you have other kinds of characters, or multiple characters (missing data, or the like), a more aggressive approach with regex may be considered:

import re
op = [x for x in map(lambda x: re.sub('\s+', ' ', x).strip(), op)]
print(op) 

['# PASS_MIN_LEN Minimum acceptable password length.', 'PASS_MIN_LEN 5']
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this pretty much works. I have a \n in between. And my xlsx file has a new line entry. '#\tPASS_MIN_LEN\tMinimum acceptable password length.\n', u'PASS_MIN_LEN\t5\n'
@COLDSPEED second regex is not handling \n. trying to get it fixed. its already 2AM.
@Sameer Are you sure it's a new line and not a literal backslash followed by n? Because that regex would handle the new line if it was actually one.
@COLDSPEED its not a literal backslash, stdout.readlines() is giving me [u'#\tPASS_MIN_LEN\tMinimum acceptable password length.\n', u'PASS_MIN_LEN\t5\n' ] And doing a "".join() on it, \n is treated as new line.
@Sameer add a flags=re.MULTILINE to the regex. [x for x in map(lambda x: re.sub('\s+', ' ', x, flags=re.M).strip(), op)]
0

You can replace the tab in the command output string with a space.

For example:

cmdOutput.replace('\t', ' ') == xlsxOutput

2 Comments

Rather than having to repeatedly do str.replace(...) on each element, why not just do it once on the entire list of strings?
Yes, calling replace on the whole list would make the most sense in this case.
0

Read the description for strip() method in official python documentation.

"Return a copy of the string with the leading and trailing characters removed."

So, the characters within the string remain unchanged. Using replace() method is the best solution for your problem.

>>> str1 = "PASS_MIN_LEN\t5"
>>> str2 = "PASS_MIN_LEN 5"
>>> str1.replace('\t', ' ') == str2
True

Comments

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.