Python 的 string.strip() 奇怪的行为
我得到一个看起来像这样的字符串:
EVENTS: RAID
Volume Set Information
Volume Set Name : ARC-1120-VOL#00
Raid Set Name : Raid Set # 00
Volume Capacity : 1.0GB
SCSI Ch/Id/Lun : 00/00/00
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
Volume Set Information
Volume Set Name : ARC-1120-VOL#01
Raid Set Name : Raid Set # 00
Volume Capacity : 5.0GB
SCSI Ch/Id/Lun : 00/00/01
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
当我完成 string.strip("EVENTS: RAID\n") 时,我得到了这个结果:
olume Set Information
Volume Set Name : ARC-1120-VOL#00
Raid Set Name : Raid Set # 00
Volume Capacity : 1.0GB
SCSI Ch/Id/Lun : 00/00/00
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
Volume Set Information
Volume Set Name : ARC-1120-VOL#01
Raid Set Name : Raid Set # 00
Volume Capacity : 5.0GB
SCSI Ch/Id/Lun : 00/00/01
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
问题:为什么“卷集信息”的 V 消失了?
正如你所看到的,我想删除第一行,如果有人知道更好的方法吗? (我知道这里有很多“Pythonic”家伙......给我你最好的机会=)
I got a string looking like that:
EVENTS: RAID
Volume Set Information
Volume Set Name : ARC-1120-VOL#00
Raid Set Name : Raid Set # 00
Volume Capacity : 1.0GB
SCSI Ch/Id/Lun : 00/00/00
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
Volume Set Information
Volume Set Name : ARC-1120-VOL#01
Raid Set Name : Raid Set # 00
Volume Capacity : 5.0GB
SCSI Ch/Id/Lun : 00/00/01
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
And when I done a string.strip("EVENTS: RAID\n"), i got this result:
olume Set Information
Volume Set Name : ARC-1120-VOL#00
Raid Set Name : Raid Set # 00
Volume Capacity : 1.0GB
SCSI Ch/Id/Lun : 00/00/00
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
Volume Set Information
Volume Set Name : ARC-1120-VOL#01
Raid Set Name : Raid Set # 00
Volume Capacity : 5.0GB
SCSI Ch/Id/Lun : 00/00/01
Raid Level : Raid5
Stripe Size : 64K
Member Disks : 3
Cache Mode : Write Back
Tagged Queuing : Enabled
Volume State : Degraded
Question: Why the V of "Volume Set Information" is disappeared ?
As you can see, I want to remove the first line, if anybody know a better way to do? (I know there is a lot of "pythonic" guys here... give me your best shot =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为
strip
的参数是要删除的字符串,而您的字符串包含“V”。你为什么还要传递那个字符串?
Because the argument of
strip
is the string of characters to remove and yours includes "V".Why are you passing it that string anyways?
您是否阅读过
strip()< 的文档/代码>
?
它会删除您提供的任意数量的字符,因此
.strip("EVENTS: RAID\n")
会删除每个E
、每个V
,每个E
,每个N
,...直到找到一个不存在的字符!这就是卷集信息
的V
丢失的原因。尝试使用
replace(string, "EVENTS: RAID\n", "", 1)
代替。Did you read the documentation of
strip()
?It strips of any number of characters that you provided, so
.strip("EVENTS: RAID\n")
strips of everyE
, everyV
, everyE
, everyN
, ... until one character is found that's not in there! This is why theV
ofVolume Set Information
goes missing.Try
replace(string, "EVENTS: RAID\n", "", 1)
instead.Strip()
将去除与您在其参数字符串中指定的字符之一匹配的所有前导字符。由于“V”是其中之一(在“事件”中),因此它被删除。您要做的就是替换开头的“EVENTS: RAID\n”。您可以为此使用正则表达式。
Strip()
will strip all the leading characters that match one of the characters you specified in its parameter string. Since 'V' is one of them (in EVENTS), it gets stripped.What you want to do is to replace the leading "EVENTS: RAID\n". You can use regular expressions for that.
string.strip()
从字符串的开头或结尾删除给定字符的所有实例。尝试一些类似的东西
,或者如果你想要一些快速而肮脏的东西,
string.strip()
removes all instances of the given characters from the string's beginning or end.Try something along the lines of
or if you want something quick and dirty,
这种行为是绝对正确的。
strip(chars) 应该删除“chars”中指定的所有尾随/前导字符。
它与执行 string.replace() 操作无关。由于“V”是在“字符”中指定的,因此您也会丢失第一个“V”。如果您不相信,请返回查看 string.strip() 文档。
The behavior is absolutely correct.
strip(chars) is supposed to chop of all trailing/leading characters as specified in 'chars'.
It is not about performing a string.replace() operation. Since 'V' is specificed in 'chars' you will lose the first 'V' as well. Please check back with the string.strip() documentation if you don't believe it.