欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何解決secure_filename對中文不支持問題

 更新時間:2021年07月15日 15:27:14   作者:waws520  
最近使用到了secure_filename,然后悲劇的發(fā)現中文居然不展示出來,本文就詳細的介紹一下解決方法,感興趣的可以了解一下

前言:最近使用到了secure_filename,然后悲劇的發(fā)現中文居然不展示出來,于是我慢慢的debug,終于找到問題了。

一、最近使用secure_filename發(fā)現的問題

文件名是中文版的,悲劇的是中文以及其他特殊字符會被省略。

二、后面找到了原因

原來secure_filename()函數只返回ASCII字符,非ASCII字符會被過濾掉。

三、解決方案

找到secure_filename(filename)函數,修改它的源代碼。

secure_filename(filename)函數源代碼:
def secure_filename(filename: str) -> str:
    r"""Pass it a filename and it will return a secure version of it.  This
    filename can then safely be stored on a regular file system and passed
    to :func:`os.path.join`.  The filename returned is an ASCII only string
    for maximum portability.

    On windows systems the function also makes sure that the file is not
    named after one of the special device files.

    >>> secure_filename("My cool movie.mov")
    'My_cool_movie.mov'
    >>> secure_filename("../../../etc/passwd")
    'etc_passwd'
    >>> secure_filename('i contain cool \xfcml\xe4uts.txt')
    'i_contain_cool_umlauts.txt'

    The function might return an empty filename.  It's your responsibility
    to ensure that the filename is unique and that you abort or
    generate a random filename if the function returned an empty one.

    .. versionadded:: 0.5

    :param filename: the filename to secure
    """
    filename = unicodedata.normalize("NFKD", filename)
    filename = filename.encode("ascii", "ignore").decode("ascii")

    for sep in os.path.sep, os.path.altsep:
        if sep:
            filename = filename.replace(sep, " ")
    filename = str(_filename_ascii_strip_re.sub("", "_".join(filename.split()))).strip(
        "._"
    )

    # on nt a couple of special files are present in each folder.  We
    # have to ensure that the target file is not such a filename.  In
    # this case we prepend an underline
    if (
        os.name == "nt"
        and filename
        and filename.split(".")[0].upper() in _windows_device_files
    ):
        filename = f"_{filename}"

    return filename

secure_filename(filename)函數修改后的代碼:

def secure_filename(filename: str) -> str:
    r"""Pass it a filename and it will return a secure version of it.  This
    filename can then safely be stored on a regular file system and passed
    to :func:`os.path.join`.  The filename returned is an ASCII only string
    for maximum portability.

    On windows systems the function also makes sure that the file is not
    named after one of the special device files.

    >>> secure_filename("My cool movie.mov")
    'My_cool_movie.mov'
    >>> secure_filename("../../../etc/passwd")
    'etc_passwd'
    >>> secure_filename('i contain cool \xfcml\xe4uts.txt')
    'i_contain_cool_umlauts.txt'

    The function might return an empty filename.  It's your responsibility
    to ensure that the filename is unique and that you abort or
    generate a random filename if the function returned an empty one.

    .. versionadded:: 0.5

    :param filename: the filename to secure
    """
    filename = unicodedata.normalize("NFKD", filename)
    filename = filename.encode("utf8", "ignore").decode("utf8")   # 編碼格式改變

    for sep in os.path.sep, os.path.altsep:
        if sep:
            filename = filename.replace(sep, " ")
    _filename_ascii_add_strip_re = re.compile(r'[^A-Za-z0-9_\u4E00-\u9FBF\u3040-\u30FF\u31F0-\u31FF.-]')
    filename = str(_filename_ascii_add_strip_re.sub('', '_'.join(filename.split()))).strip('._')             # 添加新規(guī)則

    # on nt a couple of special files are present in each folder.  We
    # have to ensure that the target file is not such a filename.  In
    # this case we prepend an underline
    if (
        os.name == "nt"
        and filename
        and filename.split(".")[0].upper() in _windows_device_files
    ):
        filename = f"_{filename}"

    return filename

四、效果展示

我們很清楚的看到了效果,目前是支持中文的

到此這篇關于Python如何解決secure_filename對中文不支持問題的文章就介紹到這了,更多相關Python secure_filename不支持中文內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python實現二分查找算法實例

    Python實現二分查找算法實例

    這篇文章主要介紹了Python實現二分查找算法,實例分析了二分查找算法的原理與相關實現技巧,需要的朋友可以參考下
    2015-05-05
  • JavaScript嵌入百度地圖API的最詳細方法

    JavaScript嵌入百度地圖API的最詳細方法

    這篇文章主要介紹了JavaScript嵌入百度地圖API的最詳細方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • python要安裝在哪個盤

    python要安裝在哪個盤

    在本篇文章里小編給大家分享的是一篇關于python必須裝在c盤嗎的知識點文章,有興趣的朋友們可以學習下。
    2020-06-06
  • python中必會的四大高級數據類型(字符,元組,列表,字典)

    python中必會的四大高級數據類型(字符,元組,列表,字典)

    這篇文章主要介紹了python中必會的四大高級數據類型(字符,元組,列表,字典),本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • python等待10秒執(zhí)行下一命令的方法

    python等待10秒執(zhí)行下一命令的方法

    在本篇文章里小編給大家整理的是關于python等待10秒執(zhí)行下一命令的方法及實例,需要的朋友們可以參考下。
    2020-07-07
  • Python?Diagrams創(chuàng)建高質量圖表和流程圖實例探究

    Python?Diagrams創(chuàng)建高質量圖表和流程圖實例探究

    Python?Diagrams是一個強大的Python庫,使創(chuàng)建這些圖表變得簡單且靈活,本文將深入介紹Python?Diagrams,包括其基本概念、安裝方法、示例代碼以及一些高級用法,以幫助大家充分利用這一工具來創(chuàng)建令人印象深刻的圖表
    2024-01-01
  • 詳解Python中對Excel的處理操作

    詳解Python中對Excel的處理操作

    Excel是一種常見的電子表格文件格式,廣泛用于數據記錄和處理,Python提供了多個第三方庫,可以方便地對Excel操作,下面就來和大家詳細講講吧
    2023-07-07
  • 利用python在excel里面直接使用sql函數的方法

    利用python在excel里面直接使用sql函數的方法

    今天小編就為大家分享一篇利用python在excel里面直接使用sql函數的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python聚類算法選擇方法實例

    python聚類算法選擇方法實例

    在本篇文章里小編給大家整理的是一篇關于python聚類算法選擇方法實例,有需要的朋友們可以學習參考下。
    2021-07-07
  • 使用python獲取cpu每秒的使用率

    使用python獲取cpu每秒的使用率

    這篇文章主要介紹了使用python獲取cpu每秒的使用率,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論