zoukankan      html  css  js  c++  java
  • 【2020Python错题本】week1错误集

     

    一、文件处理

    错误类型:

    UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence

    1、 t 模式下的 读操作

    新建txt文件 313.txt ——

    hello world
    hello day
    hello me

    @2020

    >>> f=open(r'D:tempt313.txt',mode='rt')
    >>> print(f)
    <_io.TextIOWrapper name='D:\0tempt\313.txt' mode='rt' encoding='cp936'>
    >>> res=f.read()
    >>> print(res)
    hello world
    hello day
    hello mili
    
    @2020
    
    >>> 

    新建txt文件 3133.txt——

    你好,世界
    你好,每一天
    你好,米粒

    >>> f=open(r'D:tempt3133.txt',mode='rt')
    >>> print(f)
    <_io.TextIOWrapper name='D:\0tempt\3133.txt' mode='rt' encoding='cp936'>
    >>> res=f.read()
    Traceback (most recent call last):
      File "<pyshell#46>", line 1, in <module>
        res=f.read()
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence
    >>> 

    UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence

    解决方法: 指定解码编码格式——encoding='UTF-8'

    >>> f=open(r'D:tempt3133.txt',mode='rt',encoding='UTF-8')
    >>> print(f)
    <_io.TextIOWrapper name='D:\0tempt\3133.txt' mode='rt' encoding='UTF-8'>
    >>> res=f.read()
    >>> print(res)
    你好,世界
    你好,每一天
    你好,米粒
    
    >>> 

    =====

    之前英文和数字的文本内容,读取时没有指定解码编码,没有出错。是因为英文和数字是不会出现乱码现象的,使用任何编码类型解释器都能识别英文和数字。

  • 相关阅读:
    dva实用的学习笔记
    上传图片到七牛云
    Lodash学习笔记
    Ant Design Pro 脚手架+umiJS 实践总结
    SVN的安装和使用手册
    判断数据类型的5种方法
    常见react面试题汇总(适合中级前端)
    Es6 类class的关键 super、static、constructor、new.target
    ES2019 新特性简介
    通用正则实战200
  • 原文地址:https://www.cnblogs.com/bigorangecc/p/12485615.html
Copyright © 2011-2022 走看看