简体   繁体   English

忽略python字符串中的制表符和空格

[英]Ignoring tabs and spaces in a python string

I need to compare two string in python, first string is read from .xlsx file and second is an output from stdout.readlines(). 我需要比较python中的两个字符串,第一个字符串是从.xlsx文件读取的,第二个是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 . 由于某些命令输出以\\ t开头或之间可能带有\\ t。

For Eg : Below command output begin with \\t and after LEN there is \\t. 例如:在下面的命令输出中,以\\ t开头,在LEN之后为\\ t。

#   PASS_MIN_LEN    Minimum acceptable password length.
PASS_MIN_LEN    5

And xlsx file is having 和xlsx文件有

# 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. 由于.xlsx比较字符串没有\\ t,在比较两个字符串时如何忽略\\ t。

if cmdOutput== xlsxOutput:

is not working. 不管用。

I tried to trim the cmdOutput with \\t, it didn't worked. 我试图用\\ t修剪cmdOutput,但是没有用。 Any approach can i follow? 我可以采取任何方法吗?

if you just want to replace tabs with a space, perhaps str.replace is simple enough. 如果只想用空格替换制表符,则str.replace可能很简单。 But that doesn't leave the trailing newlines. 但这并不会留下尾随的换行符。 You might consider the replacement followed by str.strip . 您可能会考虑在str.strip之后进行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 re gex may be considered: 如果您有其他种类的字符或多个字符(缺少数据等),则可以考虑使用re gex进行更积极的处理:

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']

You can replace the tab in the command output string with a space. 您可以用空格替换命令输出字符串中的选项卡。

For example: 例如:

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

Read the description for strip() method in official python documentation. 阅读官方python文档中strip()方法的描述。

"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. 使用replace()方法是解决您问题的最佳解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM