0
added = ['hypr1005.ops.colo.cmp.com,web1234.ads.colo.cmp.com,876543,4']

上面的变量是一个列表,它包含由逗号分隔的单个值,我只想使用逗号分割值并创建一个看起来像的列表

added = ['hypr1005.ops.colo.cmp.com','web1234.ads.colo.cmp.com','876543','4']

words = added.split(',')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'

Guess split 仅适用于不在列表中的字符串,那么我该怎么做呢?

4

2 回答 2

2

split列表第一项( )的调用方法added[0]

>>> added = ['hypr1005.ops.colo.cmp.com,web1234.ads.colo.cmp.com,876543,4']
>>> added = added[0].split(',')
>>> added
['hypr1005.ops.colo.cmp.com', 'web1234.ads.colo.cmp.com', '876543', '4']
于 2013-09-21T13:53:16.437 回答
0

为了使用拆分,它应该是一个字符串。

>>> added = "'hypr1005.ops.colo.cmp.com','web1234.ads.colo.cmp.com','876543','4'"
>>> words = added.split(',')
>>> words
["'hypr1005.ops.colo.cmp.com'", "'web1234.ads.colo.cmp.com'", "'876543'", "'4'"]
>>> 
于 2013-09-21T14:02:45.287 回答