Python - Unicode可拆分的东西

时间:2011-08-25 17:49:34

标签: python unicode split

我需要将日期值分解为它的元素(8/23/2011),这应该是一块蛋糕

 variable.split("/") 

但它告诉我

'unicode' object has no attribute 'Split'

我尝试将其编码为不同的格式:

date.encode("utf-8")

然后它告诉我

'str' object has no attribute 'Split'

作为Python的新手,似乎我之前使用过split with strings,但我没有把它变成正确的格式或其他东西。或许还有另一种方式更容易。

3 个答案:

答案 0 :(得分:7)

Python区分大小写;你想要split,而不是Split

>>> x = u"8/23/2011"
>>> x.Split('/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'unicode' object has no attribute 'Split'
>>> x.split('/')
[u'8', u'23', u'2011']

答案 1 :(得分:1)

Python区分大小写。该方法称为split,而不是Split

答案 2 :(得分:0)

unicode.split以小写s开头,效果很好:

>>> u'a,b'.split(u',')
[u'a', u'b']