Python错误,“模块”对象没有属性“lstrip”

时间:2021-12-05 03:02:20

Python documentation from http://docs.python.org/library/string.html :

Python文档来自http://docs.python.org/library/string.html:

string.lstrip(s[, chars])

字符串。lstrip(s(字符))

Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on."

返回删除了前导字符的字符串的副本。如果忽略字符或没有字符,则删除空白字符。如果给定而没有,字符必须是字符串;字符串中的字符将从字符串的开头被删除。

Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> import string
>>> x = 'Hi'
>>> string.lstrip(x,1)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    string.lstrip(x,1)
AttributeError: 'module' object has no attribute 'lstrip'
>>> 

What am I missing here?

我错过了什么?

5 个解决方案

#1


7  

Documentation for py3k version is located here: http://docs.python.org/py3k/index.html

py3k版本的文档在这里:http://docs.python.org/py3k/index.html

string functions were removed in py3k and you have to use now str methods:

py3k中删除了字符串函数,现在必须使用str方法:

>>> x = 'Hi'
>>> x.lstrip('H')
'i'

Note, that as documentation says, chars must be a string. Not an integer.

注意,如文档所述,chars必须是字符串。不是一个整数。

#2


3  

For Python 2.6 the following works ...

对于Python 2.6,以下工作……

import string
x = u'Hi' #needs to be unicode
string.lstrip(x,'H') #second argument needs to be char

For Python 3.0 the previous solution won't work since string.lstrip was deprecated in 2.4 and removed in 3.0.

对于Python 3.0,之前的解决方案因为字符串而无法工作。lstrip在2.4中被弃用,在3.0中被移除。

Another way is to do:

另一种方法是:

"Hi".lstrip('H')  #strip a specific char

or

" Hi".lstrip() #white space needs no input param

Which I think is the common widely use of it.

我认为这是普遍使用的。

Edit

编辑

To add deprecation of string.lstrip in Python 3.0 - thanks for the comments on this answer that mentioned it.

添加对字符串的弃用。lstrip in Python 3.0 -感谢您对这个答案的评论。

#3


1  

You have found the Python 2.7.1 version of the docs (look at the top left corner of the screen). string functions were deprecated in Python 2.x in favour of str and unicode methods, and removed totally in Python 3.x. See the 3.x docs here.

您已经找到了Python 2.7.1版本的文档(请查看屏幕左上角)。Python 2中不赞成使用字符串函数。支持str和unicode方法,并在Python 3.x中完全删除。看到3。这里x文档。

#4


0  

You don't look the good doc you are using python 3.1 the right doc is here http://docs.python.org/py3k/library/string.html

您没有使用python 3.1的好文档,正确的文档是http://docs.python.org/py3k/library/string.html。

#5


0  

This was changed for Python 3.x.

这是为Python 3.x而修改的。

The method you referring to is only available for string instances, not the module string. So you don't need to import anything:

您引用的方法仅适用于字符串实例,而不适用于模块字符串。所以你不需要导入任何东西:

 assert 'a ' == '  a '.lstrip()

#1


7  

Documentation for py3k version is located here: http://docs.python.org/py3k/index.html

py3k版本的文档在这里:http://docs.python.org/py3k/index.html

string functions were removed in py3k and you have to use now str methods:

py3k中删除了字符串函数,现在必须使用str方法:

>>> x = 'Hi'
>>> x.lstrip('H')
'i'

Note, that as documentation says, chars must be a string. Not an integer.

注意,如文档所述,chars必须是字符串。不是一个整数。

#2


3  

For Python 2.6 the following works ...

对于Python 2.6,以下工作……

import string
x = u'Hi' #needs to be unicode
string.lstrip(x,'H') #second argument needs to be char

For Python 3.0 the previous solution won't work since string.lstrip was deprecated in 2.4 and removed in 3.0.

对于Python 3.0,之前的解决方案因为字符串而无法工作。lstrip在2.4中被弃用,在3.0中被移除。

Another way is to do:

另一种方法是:

"Hi".lstrip('H')  #strip a specific char

or

" Hi".lstrip() #white space needs no input param

Which I think is the common widely use of it.

我认为这是普遍使用的。

Edit

编辑

To add deprecation of string.lstrip in Python 3.0 - thanks for the comments on this answer that mentioned it.

添加对字符串的弃用。lstrip in Python 3.0 -感谢您对这个答案的评论。

#3


1  

You have found the Python 2.7.1 version of the docs (look at the top left corner of the screen). string functions were deprecated in Python 2.x in favour of str and unicode methods, and removed totally in Python 3.x. See the 3.x docs here.

您已经找到了Python 2.7.1版本的文档(请查看屏幕左上角)。Python 2中不赞成使用字符串函数。支持str和unicode方法,并在Python 3.x中完全删除。看到3。这里x文档。

#4


0  

You don't look the good doc you are using python 3.1 the right doc is here http://docs.python.org/py3k/library/string.html

您没有使用python 3.1的好文档,正确的文档是http://docs.python.org/py3k/library/string.html。

#5


0  

This was changed for Python 3.x.

这是为Python 3.x而修改的。

The method you referring to is only available for string instances, not the module string. So you don't need to import anything:

您引用的方法仅适用于字符串实例,而不适用于模块字符串。所以你不需要导入任何东西:

 assert 'a ' == '  a '.lstrip()