亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

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

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

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

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

Python如何解決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 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)函數修改后的代碼:

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(’._’) # 添加新規則 # 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對中文不支持問題

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 中文字幕曰韩一区二区不卡 | 国产精品区一区二区免费 | 中文字幕 亚洲一区 | 久久国产自偷自免费一区100 | 日韩欧美一二三 | 精品国产亚洲一区二区三区 | 日韩精品一区二区三区国语自制 | 黄页成人免费网站 | 国产一区二区在线视频播放 | 成人精品一区二区三区 | 黄色一级性生活视频 | 久久亚洲精品成人 | 国产a级特黄的片子视频免费 | 国产免费高清视频在线观看不卡 | 久综合网 | 亚洲国产成人va在线观看 | 新香蕉视频在线 | 236理论片在线播放 24小时中文乱码字幕在线观看 | 国产在线视欧美亚综合 | 182tv午夜线路一线路二 | 久久中文字幕视频 | 真正毛片 | 国产aaa级一级毛片 国产aaa免费视频国产 | 成人毛片100部免费看 | 亚洲日韩中文字幕在线播放 | 久久青草18免费观看网站 | 午夜日b视频 | 521香蕉永久播放地址 | 国产福利免费在线观看 | 精品欧美一区视频在线观看 | 亚洲免费国产 | 亚洲精品午夜aaa级久久久久 | 日韩免费视频网站 | 妞干网免费在线 | 国产视频1| 一级免费片 | 国产精品自拍在线观看 | 欧美日韩国产成人精品 | 黄色三级网站免费 | 成人短视频在线观看 | 婷婷丁香色综合图亚洲 |