好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Python如何解決secure_filename對中文不支持問題

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

熱門標簽:長春極信防封電銷卡批發 企業彩鈴地圖標注 煙臺電話外呼營銷系統 電銷機器人錄音要學習什么 上海正規的外呼系統最新報價 外賣地址有什么地圖標注 預覽式外呼系統 銀川電話機器人電話 如何地圖標注公司

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

一、最近使用secure_filename發現的問題

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

二、后面找到了原因

原來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('._')             # 添加新規則

    # 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不支持中文內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python 中文亂碼問題深入分析
  • python中文亂碼的解決方法
  • Python使用中文正則表達式匹配指定中文字符串的方法示例
  • Python的shutil模塊中文件的復制操作函數詳解
  • python實現中文輸出的兩種方法
  • python實現中文轉換url編碼的方法
  • Python中使用中文的方法
  • 解決vscode python print 輸出窗口中文亂碼的問題
  • python中Pycharm 輸出中文或打印中文亂碼現象的解決辦法
  • wxPython中文教程入門實例

標簽:西寧 佳木斯 潮州 湖北 珠海 上饒 宜昌 盤錦

巨人網絡通訊聲明:本文標題《Python如何解決secure_filename對中文不支持問題》,本文關鍵詞  Python,如何,解決,secure,filename,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python如何解決secure_filename對中文不支持問題》相關的同類信息!
  • 本頁收集關于Python如何解決secure_filename對中文不支持問題的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 96精品久久久一区二区三区蜜桃| 亚洲女人尿尿| 成色18k1.8811.7v| 国内精品久久久久影院优| 爽?好紧?宝贝别夹大巴在线观看| 国产做a爱一级毛片久久纹身| 女子舞厅热舞周围男性看醉了| 亚洲三级色| 欧美日韩成人在线| 中文字幕亚洲欧美日韩高清| 国产一级a一片| 韩国女式开襟睡衣视频| 2021国产精品久久| 男女叉叉视频试看三分钟| 日韩中文无码免费| 嗯啊好大好深| 扒开双腿猛进入爽爽视频ai| 女人私密又大又肥好吗| 欧美三级做爰全过程| 青青久久久国产线免观| 啊?日出水了?用力乖乖口述| 老司机深夜福利视频| 性生交大片1978| 成年美女黄网站在线观看| 丰满肥妇bbwbbwbbwbbw| 操淫| 偷窥女厕正面小便| a级国产乱理论片在线观看看| videos欧美丰满肥婆| xxxxxhd亚洲日本hd| 色噜噜狠狠色综合免费视频| 亚洲欧美日韩一区超高清| 性欲超市| Free HD Cartoon XXX Videos| 欧美一级毛片,免费| 欧美乱大交xxxxx在线观看| 国产肥臀久久?爆乳奶萝?| 男与女的韩国电影| 金瓶悔1一5扬思敏完正版| 97热久久| 免费看黄页|