我总是在文件名中遇到特殊字符和 Ä、Ö、Ü 的问题。到目前为止,我已经收集并替换了字符。但总是有新的,而且它根本不是自动化的
例如文件名:
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] 删除。
我来说两句