String 我应该如何更改此函数,使其能够在Python2和Python3中灵活地处理字符串和unicode?

String 我应该如何更改此函数,使其能够在Python2和Python3中灵活地处理字符串和unicode?,string,python-3.x,unicode,String,Python 3.x,Unicode,我有一个向一个或多个收件人发送文本消息和文件的功能。有一个全局发送者对象,它在Python2中作为unicode对象接受文本和接收者配置,在Python3中作为字符串对象接受文本和接收者配置。以下是与Python 2兼容的函数: def send_message_Telegram( recipient = None, # string recipients = None, # list of strings text = None, filepath

我有一个向一个或多个收件人发送文本消息和文件的功能。有一个全局发送者对象,它在Python2中作为unicode对象接受文本和接收者配置,在Python3中作为字符串对象接受文本和接收者配置。以下是与Python 2兼容的函数:

def send_message_Telegram(
    recipient  = None, # string
    recipients = None, # list of strings
    text       = None,
    filepath   = None
    ):

    if text and not filepath:
        if recipient:
            tg_sender.send_msg(
                unicode(recipient),
                unicode(text)
            )
        if recipients:
            for recipient in recipients:
                tg_sender.send_msg(
                    unicode(recipient),
                    unicode(text)
                )
    if filepath and not text:
        if recipient:
            tg_sender.send_file(
                unicode(recipient),
                unicode(filepath)
            )
        if recipients:
            for recipient in recipients:
                tg_sender.send_file(
                    unicode(recipient),
                    unicode(filepath)
                )
如果希望此函数与Python 3兼容,我必须将
unicode()
的所有用法更改为
str()
。我需要在Python2和Python3中都使用该函数,那么应该如何更改它呢?我不想到处写这样的代码:

if sys.version_info >= (3, 0):
    tg_sender.send_msg(
        str(recipient),
        str(text)
    )
else:
    tg_sender.send_msg(
        unicode(recipient),
        unicode(text)
    )
就像,这是我现在最好的,但看起来很奇怪:

def ustr(text):

    if text is not None:
        if sys.version_info >= (3, 0):
            return str(text)
        else:
            return unicode(text)
    else:
        return text

def send_message_Telegram(
    recipient  = None, # string
    recipients = None, # list of strings
    text       = None,
    filepath   = None
    ):

    if text and not filepath:
        if recipient:
            tg_sender.send_msg(
                ustr(recipient),
                ustr(text)
            )
        if recipients:
            for recipient in recipients:
                tg_sender.send_msg(
                    ustr(recipient),
                    ustr(text)
                )
    if filepath and not text:
        if recipient:
            tg_sender.send_file(
                ustr(recipient),
                ustr(filepath)
            )
        if recipients:
            for recipient in recipients:
                tg_sender.send_file(
                    ustr(recipient),
                    ustr(filepath)
                )

您可以创建一个函数来安全地强制转换字符串

这就是我的解决方案:

def string(data):
    if sys.version_info >= (3, 0):
        return str(data)
    else:
        return unicode(data)

您可以创建一个函数来安全地强制转换字符串

这就是我的解决方案:

def string(data):
    if sys.version_info >= (3, 0):
        return str(data)
    else:
        return unicode(data)

根据版本,函数的别名如何

def send_message_Telegram(recipient  = None, 
                          recipients = None, 
                          text = None,
                          filepath = None):
    if sys.version_info[0] < 3:
        func = unicode
    else:
        func = str

    # the rest here, using 'func()'
def发送消息电报(收件人=无,
收件人=无,
text=无,
文件路径=无):
如果系统版本信息[0]<3:
func=unicode
其他:
func=str
#其余的在这里使用'func()'

根据版本,函数的别名如何

def send_message_Telegram(recipient  = None, 
                          recipients = None, 
                          text = None,
                          filepath = None):
    if sys.version_info[0] < 3:
        func = unicode
    else:
        func = str

    # the rest here, using 'func()'
def发送消息电报(收件人=无,
收件人=无,
text=无,
文件路径=无):
如果系统版本信息[0]<3:
func=unicode
其他:
func=str
#其余的在这里使用'func()'
如果您已经安装,您可以使用它的
文本类型
在2.x中引用
unicode
,在3.x中引用
str

如果您已经安装,您可以使用它的
文本类型
在2.x中引用
unicode
,在3.x中引用
str