AttributeError: '_io.TextIOWrapper' object has no attribute 'xreadlines'
Python 错误!AttributeError: '_io.TextIOWrapper' object has no attribute 'xreadlines'
Traceback (most recent call last):
File "countline.py", line 33, in <module>
totalline = totalline + countLine(filelist)
File "countline.py", line 23, in countLine
for file_line in open(fname).xreadlines():
AttributeError: '_io.TextIOWrapper' object has no attribute 'xreadlines'
原因:
- 在Python 2里,文件对象有一个xreadlines()方法,它返回一个迭代器,一次读取文件的一行。这在for循环中尤其有用。事实上,后来的Python 2版本给文件对象本身添加了这样的功能。
- 在Python 3里,xreadlines()方法不再可用了。
解决方法:
for file_line in open(fname).xreadlines():
改为:for file_line in open(fname, encoding="utf-8").readlines():
加上utf-8就不会乱码了。
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
若有帮助到您,欢迎点击推荐,您的支持是对我坚持最好的肯定(*^_^*)
你要保守你心,胜过保守一切。
本文来自博客园,作者:刘俊涛的博客,转载请注明原文链接:https://www.cnblogs.com/lovebing/p/13048948.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2019-06-05 Hide()方法不生效