簡體   English   中英

我應該如何更改此函數,使其可以在Python 2和Python 3中整齊地處理字符串和unicode?

[英]How should I change this function such that it can neatly handle strings and unicode in both Python 2 and in Python 3?

我有一個功能,可以將文本消息和文件發送給一個或多個收件人。 有一個全局發送者對象,它接受文本和接收者配置作為Python 2中的unicode對象和Python 3中的字符串對象。這是目前的功能,並且與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() 我需要在Python 2和Python 3中都可以使用的函數,那么應該如何更改呢? 我不想到處寫這樣的代碼:

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 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()'

如果安裝six模塊 ,則可以使用其text_type來引用2.x中的unicode和3.x中的str

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM