python的编解码:

  input文件(gbk, utf-8...)   ----decode----->   unicode  -------encode------> output文件(gbk, utf-8...)

很多文件处理的包是unicode编码,刚开始读入文件(gbk, utf-8...)后要decode为unicode编码格式,再encode为需要

的格式(gbk, utf-8...)。

第一种 用open():

  f=open('xxx', 'r')
  content=f.read().decode('utf-8')

第二种 用codecs.open():

  f=codecs.open(XXX,'r', encoding='utf-8') #使用codecs.open读入时直接解码
  content=f.read()

------2016-12-23--------------------------------------

问题:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence

  原因可能是文本里有奇怪的符号比如:®,?(英文的问号)( 在学习机器学习实战ham/23.txt这本书时,遇到®符号问题)

 

相关文章:

  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2021-10-16
  • 2021-08-01
  • 2022-01-12
  • 2022-01-11
  • 2021-05-23
猜你喜欢
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2021-05-16
相关资源
相似解决方案