我想从文件夹中的所有编码文件中删除所有重音符号..我已经在构建文件列表中成功,问题是当我尝试使用unicodedata进行标准化我得到的错误: **回溯(最近一次调用最后一次): 文件“/usr/lib/gedit-2/plugins/pythonconsole/console.py”,第336行,在__run中 exec command in self .namespace 文件 “”,第2行,在 UnicodeDecodeError错误:在位置25 'UTF8' 编解码器不能解码字节0xf3:无效延续字节 **Python - 从文件夹中的所有文件中删除重音
if options.remove_nonascii:
nERROR = 0
print _("# Removing all acentuation from coding files in %s") % (options.folder)
exts = ('.f90', '.f', '.cpp', '.c', '.hpp', '.h', '.py'); files=set()
for dirpath, dirnames, filenames in os.walk(options.folder):
for filename in (f for f in filenames if f.endswith(exts)):
files.add(os.path.join(dirpath,filename))
for i in range(len(files)):
f = files.pop() ;
os.rename(f,f+'.BACK')
with open(f,'w') as File:
for line in open(f+'.BACK').readlines():
try:
newLine = unicodedata.normalize('NFKD',unicode(line)).encode('ascii','ignore')
File.write(newLine)
except UnicodeDecodeError:
nERROR +=1
print "ERROR n %i - Could not remove from Line: %i" % (nERROR,i)
newLine = line
File.write(newLine)
谢谢..使用unidecode解决了! – canesin 2011-02-08 17:18:18