色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:97日期:2022-06-15 08:21:20
目錄一、最近使用secure_filename發(fā)現(xiàn)的問題二、后面找到了原因三、解決方案四、效果展示

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

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

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

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

二、后面找到了原因

原來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 xfcmlxe4uts.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 filenameand 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 xfcmlxe4uts.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-u9FBFu3040-u30FFu31F0-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 filenameand filename.split('.')[0].upper() in _windows_device_files ):filename = f'_{filename}' return filename四、效果展示

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

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲综合在线另类色区奇米 | bt天堂国产亚洲欧美在线 | 精品国产_亚洲人成在线高清 | 视频三区精品中文字幕 | 久久精品2 | 亚洲第一免费播放区 | 欧美成人中文字幕 | 免费高清毛片在线播放视频 | 一区二区三区高清在线 | 深夜福利国产 | 8888奇米四色在线 | 中国女警察一级毛片视频 | 国产99视频免费精品是看6 | 国产女乱淫真高清免费视频 | 成年午夜性视频免费播放 | 国产精品视频男人的天堂 | 亚洲欧美大片 | 韩日一级视频 | 玖草视频在线观看 | 美女黄色一级片 | 久草资源在线视频 | 欧美1| 国产初高中生粉嫩无套第一次 | 日韩经典欧美精品一区 | 亚洲国产影视 | 九九99久久精品国产 | 最新国产三级 | 国产成人一区二区三中文 | 91成人免费版 | 国产东北色老头老太性视频 | free性chinese国语对白 | 精品综合在线 | 在线看a级片 | 一级毛片 在线播放 | 三级黄色毛片网站 | 亚洲一级毛片免费在线观看 | 午夜在线伦理福利视频 | 一区二区三区免费视频网站 | 黑人黄色毛片 | 久久国产免费观看精品3 | 成视频年人黄网站免费 |