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)
    你好,世界
    你好,每一天
    你好,米粒
    
    >>> 

    =====

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

  • 相关阅读:
    浅读《构建之法》
    def 和 lamdba的区别
    Numpy和Pandas的区别
    Django:每点击一次就增加行可输入的表格
    F函数和Q函数的作用
    super()的用法
    liunx操作系统
    celery(超详细)
    celery
    FastDFS环境搭建
  • 原文地址:https://www.cnblogs.com/bigorangecc/p/12485615.html
Copyright © 2011-2022 走看看