Pycharm seek() 中文报错,要怎么做才能不报错?

Pycharm  seek() 中文报错,要怎么做才能不报错?

问题描述:

with open("a.txt","r",encoding="utf-8") as f:
    print("文件名是:{0}".format(f.name))
    print(f.tell())
    print("读取的内容:{0}".format(str(f.readline())))
    print(f.tell())
    f.seek(3)
    print("读取的内容:{0}".format(str(f.readline())))
    print(f.tell())
    print("读取的内容:{0}".format(str(f.readline())))
    print(f.tell())

seek()   要怎么做才能不报错?

中文报错  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x98 in position 0: invalid start byte

 

 

把utf-8改成这个

utf-8-sig

试试

""" 
@Time    : 2021/6/29 23:41
@Author  : Keep Doing this(小小的鹏弟)
@FileName: 测试.py
@SoftWare: PyCharm
"""
with open("a.txt","r",encoding="utf-8") as f:
    print(f.name)
    print(f.tell())
    print("{0}".format(str(f.readline())))
    print(f.tell())
    f.seek(14)
    print("{0}".format(str(f.readline())))
    print(f.tell())
    print("{0}".format(str(f.readline())))

img