Python 的 string.strip() 奇怪的行为

发布于 2024-11-04 20:08:08 字数 1422 浏览 3 评论 0原文

我得到一个看起来像这样的字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慵挽 2024-11-11 20:08:09

因为 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?

尴尬癌患者 2024-11-11 20:08:09

您是否阅读过 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 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.

童话里做英雄 2024-11-11 20:08:09

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.

别低头,皇冠会掉 2024-11-11 20:08:09

string.strip() 从字符串的开头或结尾删除给定字符的所有实例。

尝试一些类似的东西

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

,或者如果你想要一些快速而肮脏的东西,

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

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:])
爱你是孤单的心事 2024-11-11 20:08:09

这种行为是绝对正确的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文