开发者

Python's string.strip() weird behavior

开发者 https://www.devze.com 2023-03-03 07:14 出处:网络
I got a string looking like that: EVENTS: RAID Volume Set Information Volume Set Name : ARC-1120-VOL#00

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  : En开发者_如何学Pythonabled
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 =)


Because the argument of strip is the string of characters to remove and yours includes "V".

Why are you passing it that string anyways?


Did you read the documentation of strip()?

It strips of any number of characters that you provided, so .strip("EVENTS: RAID\n") strips of every E, every V, every E, every N, ... until one character is found that's not in there! This is why the V of Volume Set Information goes missing.

Try replace(string, "EVENTS: RAID\n", "", 1) instead.


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() removes all instances of the given characters from the string's beginning or end.

Try something along the lines of

linebreak_pos = string.find("\n")
if linebreak_pos != -1:
    string = string[linebreak_pos:]

or if you want something quick and dirty,

string = "\n".join(string.split("\n")[1:])


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消