#below是在Python 2.6.6版本中删除搞笑字符“½”的步骤,效果很好。
#-*- coding: utf-8 -*-
import os,glob
funny=glob.glob('C:\A\Text\*') #This folder has 10 files, so i use '*' for a loop
for h in funny:
with open(r'%s' %h, 'r') as infile,open(r'%sN' %h, 'w') as outfile:
data = infile.read()
data = data.replace ("13½","13")
data = data.decode("ascii", "ignore")
outfile.write(data)
infile.close()
outfile.close()
os.remove(h)
os.rename(r'%sN' %h,r'%s' %h)
但是现在我们升级到 3.9.2 版本,这不起作用,它显示以下错误消息:
回溯(最近一次调用):文件“C:/A/test.py”,第 10 行,在 data = infile.read() 文件“C:\Program Files\Python39\lib\encodings\cp1252.py”中,第 23 行,在 decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10871: character maps to
我搜索了很多,新版本中没有用那个“½”替换功能,有什么想法吗?