最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python如何解決secure_filename對(duì)中文不支持問題

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

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

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

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

二、后面找到了原因

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

三、解決方案

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

secure_filename(filename)函數(shù)源代碼:
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)函數(shù)修改后的代碼:

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

四、效果展示

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

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

相關(guān)文章

  • 淺析python繼承與多重繼承

    淺析python繼承與多重繼承

    在本篇文章中我們給大家分析了python繼承與多重繼承的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的讀者們參考下。
    2018-09-09
  • 詳解Python中的strftime()方法的使用

    詳解Python中的strftime()方法的使用

    這篇文章主要介紹了詳解Python中的strftime()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法

    Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法

    今天小編就為大家分享一篇Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 女友半夜加班發(fā)自拍 python男友用30行代碼發(fā)現(xiàn)驚天秘密

    女友半夜加班發(fā)自拍 python男友用30行代碼發(fā)現(xiàn)驚天秘密

    大家好,我是Lex 喜歡欺負(fù)超人那個(gè)Lex 女友說今晚加班,還給我發(fā)了一張照片? 我心生懷疑,就用python分析了一下照片,結(jié)果發(fā)現(xiàn)。。。 劃重點(diǎn):利用Python讀取照片的GPS信息信息
    2021-08-08
  • Python 線程池用法簡(jiǎn)單示例

    Python 線程池用法簡(jiǎn)單示例

    這篇文章主要介紹了Python 線程池用法,結(jié)合簡(jiǎn)單實(shí)例形式分析了Python線程池相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2019-10-10
  • python3注冊(cè)全局熱鍵的實(shí)現(xiàn)

    python3注冊(cè)全局熱鍵的實(shí)現(xiàn)

    這篇文章主要介紹了python3注冊(cè)全局熱鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 利用python Pandas實(shí)現(xiàn)批量拆分Excel與合并Excel

    利用python Pandas實(shí)現(xiàn)批量拆分Excel與合并Excel

    今天帶大家學(xué)習(xí)利用python Pandas實(shí)現(xiàn)批量拆分Excel與合并Excel,文中有非常詳細(xì)的的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 在VSCode中配置Python開發(fā)環(huán)境的詳細(xì)教程

    在VSCode中配置Python開發(fā)環(huán)境的詳細(xì)教程

    Visual Studio Code(簡(jiǎn)稱VSCode)以其強(qiáng)大的功能和靈活的擴(kuò)展性,成為了許多開發(fā)者的首選,本文將詳細(xì)介紹如何在VSCode中配置Python開發(fā)環(huán)境,需要的朋友可以參考下
    2025-04-04
  • 淺析python標(biāo)準(zhǔn)庫(kù)中的glob

    淺析python標(biāo)準(zhǔn)庫(kù)中的glob

    glob 文件名模式匹配,不用遍歷整個(gè)目錄判斷每個(gè)文件是不是符合。這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù)中的glob的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2020-03-03
  • python常見的占位符總結(jié)及用法

    python常見的占位符總結(jié)及用法

    在本篇文章里小編給大家整理的是一篇關(guān)于python常見的占位符總結(jié)及用法,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-07-07

最新評(píng)論

陆良县| 古丈县| 鄂托克前旗| 乐东| 独山县| 金沙县| 怀来县| 西宁市| 通城县| 察雅县| 泽普县| 唐河县| 阿勒泰市| 盐池县| 福安市| 凌云县| 平顺县| 宁武县| 金湖县| 运城市| 景谷| 广河县| 新兴县| 东至县| 衢州市| 香港 | 廉江市| 灵武市| 孝感市| 峡江县| 荣昌县| 桃园县| 利辛县| 旬阳县| 临泉县| 三门县| 绩溪县| 会东县| 普格县| 浦东新区| 黑山县|