我有一个字符串s
,我想从中删除'.mainlog'
。我试过:
>>> s = 'ntm_MonMar26_16_59_41_2018.mainlog'
>>> s.strip('.mainlog')
'tm_MonMar26_16_59_41_2018'
为什么n
会从'ntm...'
中删除
同样,我还有另一个问题:
>>> s = 'MonMar26_16_59_41_2018_rerun.mainlog'
>>> s.strip('.mainlog')
'MonMar26_16_59_41_2018_reru'
为什么python坚持要从我的字符串中删除n
?如何正确地从字符串中删除.mainlog
来自Python文档:
https://docs.python.org/2/library/string.html#string.strip
当前,它尝试剥离您提到的所有字符('.'、'm'、'a'、'i'…)
可以改用string.replace
如果您阅读^{} 的文档,您将看到:
因此
'.mainlog'
(['.', 'm', 'a', 'i', 'n', 'l', 'o', 'g']
)中的所有字符都从开头和结尾剥离您想要的是^{} 将所有出现的
'.mainlog'
替换为空:您使用了错误的函数
strip
删除字符串开头和结尾的字符。默认情况下为空格,但可以提供要删除的字符列表您应该使用:
或:
相关问题 更多 >
编程相关推荐