2011-10-06 89 views
1

我是Elkner's How To Think的自学Python,已经到了第7章,字符串。我试图通过键入进口字符串加载字符串模块,它似乎工作,但是这是我得到:字符串模块出错,python os x

>>> 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。

回答

4

你称之为“string.py”,它暗示了实际的string模块。重命名或删除它。

0

试试这个求助:

import string 

help(string) 

在Python中,你不需要输入您可以直接使用内置函数的字符串模块。

"str".function() 

,并为你的目标,你可以使用:

"str".expandtabs(n) 

8是缺省值。