Python-替换或去除不能用于文件名的字符,主要用到了正则表达式。
import re
def validateTitle(title):
rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |'
new_title = re.sub(rstr, "_", title) # 替换为下划线
print(new_title)
return new_title
validateTitle('Line / Line')
来自:
1.https://blog.csdn.net/qq_29303759/article/details/81944733
2.https://www.polarxiong.com/archi ... AD%97%E7%AC%A6.html
如果报错:TypeError: expected string or bytes-like object
则说明需要转换的不是字符串,修改:转换数据结构,加入str()。可以输出type看看本来的结构是什么,最后只要修改成str就好啦!
|