python open打开文件报错 ‘utf-8’ codec can’t decode byte 0xb2 in position 49: invalid start byte
解决方法:
1、操作字符为xb2
2、手动查看csv文件发现特殊字符
²?,OS,װ»?,»???¹?ì»??׃,ASW±,̹Ԑ²?ڵ䋽,½ڵ䆤׃,ȸԲ
3、分析报错字符编码
def all_encodings():
modnames = set([modname for importer, modname, ispkg in pkgutil.walk_packages(
path=[os.path.dirname(encodings.__file__)], prefix='')])
aliases = set(encodings.aliases.aliases.values())
return modnames.union(aliases)
text = b'\xb2'
for enc in all_encodings():
try:
msg = text.decode(enc)
except Exception:
continue
if msg in '²?,OS,װ»?,»???¹?ì»??׃,ASW±,̹Ԑ²?ڵ䋽,½ڵ䆤׃,ȸԲ':
print('Decoding {t} with {enc} is {m}'.format(t=text, enc=enc, m=msg))
输出如下
Decoding b'\xb2' with charmap is ²
Decoding b'\xb2' with koi8_t is ²
Decoding b'\xb2' with iso8859_13 is ²
Decoding b'\xb2' with raw_unicode_escape is ²
Decoding b'\xb2' with cp1253 is ²
Decoding b'\xb2' with iso8859_3 is ²
Decoding b'\xb2' with cp1258 is ²
Decoding b'\xb2' with iso8859_8 is ²
Decoding b'\xb2' with unicode_escape is ²
Decoding b'\xb2' with cp1252 is ²
Decoding b'\xb2' with iso8859_9 is ²
依次测试使用该编码打开文件
with open('filename.csv','r',encoding= 'charmap') as csvfile:
print(csvfile)
问题解决