去除字符串中的特殊字符

创建日期: 2022-11-01 16:59 | 作者: 风波 | 浏览次数: 14 | 分类: Python
import string

filepath = '/a/b/cja#!$Y^%vfjb()&(-ew.)'  # 这个文件名不可用
pathname = os.path.dirname(filepath)
basename = os.path.basename(filepath)

punctuation = string.punctuation + " " 
punctuation = punctuation.replace(".", '')
for c in punctuation:
    basename = basename.replace(c, "_")

filename = os.path.join(pathname, basename)
14 浏览
8 爬虫
0 评论