【发布时间】:2017-11-22 16:13:44
【问题描述】:
我有一个代码 sn-p 可以将葡萄牙语文本与数字分开。代码是:
import re
def name():
text = u'Obras de revisão e recuperação (45453000-7)'
splits = text.split(u" (")
return(str(splits[0].encode("utf8")))
name()
输出为:'Obras de revis\xc3\xa3o e recupera\xc3\xa7\xc3\xa3o'
但是当我写的时候
print(splits[0].encode("utf8"))
输出将是:Obras de revisão e recuperação,这是我想要的结果。
但它不适用于返回函数。我阅读了difference between __str__ and __repr__,但我仍然不知道如何通过函数内部的返回获得与__str__ 相同的输出。
【问题讨论】:
-
你说它不起作用是什么意思?发生什么了 ?你预计会发生什么?
-
只需将您的函数包装在
print()中。例如:print(name())
标签: string python-2.7 python-unicode representation