Python';s string.strip()奇怪的行为
我有一根这样的线:Python';s string.strip()奇怪的行为,python,string,strip,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
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”的家伙…给我你最好的机会=)因为
strip
的参数是要删除的字符串,而你的参数包括“V”
你为什么要传递那个字符串?你读了吗
它会删除您提供的任意数量的字符,因此.strip(“事件:RAID\n”)
会删除每个E
、每个V
、每个E
、每个n
、。。。直到找到一个不在里面的角色!这就是为什么卷集信息的V
丢失的原因
请尝试替换(字符串,“事件:RAID\n”,“1”)
。string.strip()
从字符串的开头或结尾删除给定字符的所有实例
试着做一些类似的事情
linebreak_pos = string.find("\n")
if linebreak_pos != -1:
string = string[linebreak_pos:]
或者如果你想要一些又快又脏的东西
string = "\n".join(string.split("\n")[1:])
Strip()
将剥离与您在其参数字符串中指定的字符之一匹配的所有前导字符。因为“V”是其中之一(在事件中),所以它会被剥离
您要做的是替换前导的“事件:RAID\n”。您可以使用正则表达式来实现这一点。该行为绝对正确 按照“字符”中的规定,条带(字符)应切掉所有尾随/前导字符。
这与执行string.replace()操作无关。由于“V”在“chars”中有详细说明,因此您也将丢失第一个“V”。如果你不相信它,请用String .Strand()文档检查。条只删除V,因为它只删除主角和尾随字符-而不是在字符串中间的字符。好的,谢谢。我原以为它会删除卷的所有V,但我误解了文档。