重命名文件名中的多个特殊字符

酷酷的

我总是在文件名中遇到特殊字符和 Ä、Ö、Ü 的问题。到目前为止,我已经收集并替换了字符。但总是有新的,而且它根本不是自动化的

例如文件名:

testç.txt
aaaä.txt
gggggggç.txt
rrérr.txt
-Î¥tr.pdf

例如这里:

def rename():
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ç', 'c')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ä', 'ae')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ç', 'c')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ä', 'ae')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('Ä', 'ae')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('Ö', 'oe')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ö', 'oe')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('Ü', 'ue')) for f in os.listdir(dirpath)]
    [os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f).replace('ü', 'ue')) for f in os.listdir(dirpath)]
    etc....

是否有一种简单的可能性来替换目录中所有文件中的所有特殊字符和 Ä、Ö、Ü?

拉克什

您可以使用unicodedata模块

前任:

import unicodedata
os.rename(os.path.join(dirpath, f), os.path.join(dirpath, unicodedata.normalize('NFKD', f).encode('ascii', 'ignore').decode())) for f in os.listdir(dirpath)]

演示:

import unicodedata
s = ['testç.txt', 'aaaä.txt', 'gggggggç.txt', 'rrérr.txt', '-Î¥tr.pdf']

for i in s:
    print(unicodedata.normalize('NFKD', i).encode('ascii', 'ignore').decode())

输出:

testc.txt
aaaa.txt    
gggggggc.txt
rrerr.txt   
-Itr.pdf  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从文件名中删除特殊字符

尝试重命名文件夹中的文件(基于文件名中的字符串)

如何使用文件名中的现有Unix时间重命名多个文件?

从文件名中删除特殊字符

重命名文件名

如何使用文件名重命名多个multi-fasta文件中的标题?

如何将文件重命名为文件名中的子字符串?

我想重命名Node.js中多个文件名中的特定字符串

从文件名中删除非字母数字字符并在Python中重命名

PowerShell重命名多个文件,文件名的特定部分

如何通过替换文件名中的字符串来重命名多个文件?该字符串包含“#”

重命名多个文件以从文件名中删除通用后缀

重命名文件中的命令特殊字符

根据文件名在python中重命名文件

如何在Windows cmd中重命名文件名时忽略文件名开头的字符?

如何匹配文件名中的字符串并根据字符串重命名文件?

如何通过在Windows中的文件名之间添加通用名来重命名多个文件?

重命名多个目录中的文件名

用bash重命名多个文件名

重命名多个文件夹中的多个文件名

计算文件名的字符并重命名

尝试重命名项目所有目录中的多个文件名

使用PowerShell重命名或删除文件名中的字符

脚本从多个目录中删除中间字符,然后重命名子目录中的文件以包括目录+文件名

将文件名中的特殊字符重命名为下划线

重命名文件夹中的文件名

如何通过移动/替换字符并在文件名中转换日期格式来重命名bash中的多个文件?

在 PowerShell 中重命名文件名

如何重命名文件夹中的文件名?