【发布时间】:2011-02-08 16:05:21
【问题描述】:
我正在尝试从文件夹中的所有编码文件中删除所有重音符号。我已经成功构建文件列表,问题是当我尝试使用 unicodedata 进行规范化时出现错误: ** Traceback(最近一次通话最后一次): 文件“/usr/lib/gedit-2/plugins/pythonconsole/console.py”,第 336 行,在 __run self.namespace 中的 exec 命令 文件“”,第 2 行,在 UnicodeDecodeError:“utf8”编解码器无法解码位置 25 中的字节 0xf3:无效的继续字节 **
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)
【问题讨论】:
标签: python filesystems