为什么?str.lstrip公司对于以e开头的单词,是否会截断字母e,而对于其他字母则不会?

2024-05-13 21:59:58 发布

您现在位置:Python中文网/ 问答频道 /正文

奇怪的是,当我试图用Python使用xlrd模数从excel文件中取出内容时遇到了这个问题。你知道吗

示例如下:

Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="text:u'ethos'"
>>> a
"text:u'ethos'"
>>> a.lstrip("text:u'").rstrip("'")
'hos'
>>> b="text:u'weekend'"
>>> b
"text:u'weekend'"
>>> b.lstrip("text:u'").rstrip("'")
'weekend'

正在读取xlrd的单元格文本:u“”格式,我只需要获取单词。你知道吗

虽然我最后用了表.u值()解决方法。但我想知道为什么?你知道吗

那么,为什么“周末”效果很好,但“风气”这样的词是错误的呢?你知道吗


Tags: 文件textdefault示例内容excelmargcc
1条回答
网友
1楼 · 发布于 2024-05-13 21:59:58

您应该仔细看看lstrip函数https://docs.python.org/2/library/stdtypes.html#str.lstrip

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

它最后去掉字符t、e、x、:、和u,直到到达另一个字符。“为”文本:u‘ethos’,‘h’之前的每个字符都在该列表中,因此它会删除它们。带“文本:w“weekend”,因为“w”不在该列表中,所以它停止在那里剥离字符。你知道吗

相关问题 更多 >