|
本帖最后由 Mi思特陈 于 2019-1-9 23:47 编辑
#!coding:utf-8
import linecache
path = r"C:\Users\Admin\Desktop\***************************.txt"
with open(path, 'rU', encoding='gbk') as p:
linecache.getline(p, 17009)
[size=10.6667px]Traceback (most recent call last): File "C:/Users/Admin/Desktop/project3/1111111.py", line 5, in <module> linecache.getline(p, 17009) File "D:\python367\lib\linecache.py", line 16, in getline lines = getlines(filename, module_globals) File "D:\python367\lib\linecache.py", line 47, in getlines return updatecache(filename, module_globals) File "D:\python367\lib\linecache.py", line 90, in updatecache if not filename or (filename.startswith('<') and filename.endswith('>')):AttributeError: '_io.TextIOWrapper' object has no attribute 'startswith'
这么两行代码,为啥会报一个这样的错误呢???
后来重新测试了一个小文件,文件里面有中英文,但是中文处理不了。
# -*- coding:utf-8 -*-
import linecache
path = r"C:\Users\Admin\Desktop\新建文本文档.txt"
print(linecache.getline(path, 3))
报错: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 12: invalid continuation byte
linecache.getline 应该如何处理中文字符呢??
|
|