string.split 错误?python

2 投票
5 回答
18631 浏览
提问于 2025-04-16 11:36
a = string.split("Test Test2 Test3"," ")

这段代码出现了错误:

Message File Name   Line    Position    
Traceback               
    <module>    C:\pyWiz.py 43      
AttributeError: 'module' object has no attribute 'split'                

是的,我已经导入了字符串模块。为什么会这样呢?

5 个回答

1

为什么不直接用 "Test Test2 Test3".split() 呢?

3

这里提到的 string 是一个模块,而你其实想找的是类/类型对象 str。不过,我建议你可以这样做:

a = "Test Test2 Test3".split()
8

使用:

a = 'Test Test2 Test3'.split(' ')

(也就是说,使用 str 类型的 split 方法)。在 Python 2.x 版本中,string.split 已经不推荐使用,而在 3.x 版本中则完全被移除了。

撰写回答