找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2971|回复: 0

[求助] linecache.getline()报错'_io.TextIOWrapper' object has no attribute 'startsw...

6

主题

15

帖子

15

积分

贫民

积分
15
Mi思特陈 发表于 2019-1-9 21:10:59 | 显示全部楼层 |阅读模式
本帖最后由 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'

这么两行代码,为啥会报一个这样的错误呢???



后来重新测试了一个小文件,文件里面有中英文,但是中文处理不了。
QQ图片20190109234723.png
# -*- 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 应该如何处理中文字符呢??
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表