【问题标题】:Error with string module, python os x字符串模块出错,python os x
【发布时间】:2011-12-01 22:55:07
【问题描述】:

我正在从 Elkner 的 How To Think 中自学 Python,并且已经学习到第 7 章,字符串。我尝试通过键入 import string 来加载字符串模块,它似乎可以工作,但这就是我得到的:

>>> import string
>>> dir (string)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'string']

而且,令人困惑的是,我的帮助功能似乎由于某些相关原因而死了:

>>> help()
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'string']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 458, in __call__
import pydoc
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.py", line 57, in <module>
from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
ImportError: cannot import name expandtabs

我做错了什么?我怎样才能解决这个问题?运行 Mac OS X 10.7.1。

【问题讨论】:

    标签: python macos module osx-lion


    【解决方案1】:

    试试这个寻求帮助:

    import string
    
    help(string)
    

    在python中你不需要导入字符串模块你可以直接使用内置函数。

    "str".function()
    

    对于您的目标,您可以使用:

    "str".expandtabs(n)
    

    8 是默认数字。

    【讨论】:

      【解决方案2】:

      您调用了“string.py”,它隐藏了实际的string 模块。重命名或删除它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 2016-09-25
        • 2016-02-16
        相关资源
        最近更新 更多