CSDN:http://blog.csdn.net/kicilove/article/
github:https://github.com/zhaohuicici?tab=repositories
問題:
原因:
AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘xreadlines’
I/O方法xreadlines()
- 在Python 2裏,文件對象有一個xreadlines()方法,它返回一個迭代器,一次讀取文件的一行。這在for循環中尤其有用。事實上,後來的Python 2版本給文件對象本身添加了這樣的功能。
- 在Python 3裏,xreadlines()方法不再可用了。2to3可以解決簡單的情況,但是一些邊緣案例則需要人工介入。
如果你以前使用一個參數(每次讀取的行數)調用xreadlines(),2to3不能爲你完成從Python 2到Python 3的轉換,你的代碼會以這樣的方式失敗:AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘xreadlines’。
解決方法:
你可以
f= open('train.txt','rb')
#將xreadlines改成readlines之後
print (f.readlines())
for line in f.readlines() :
print (line)
f.close()
'''
#部分結果,發現亂碼,這個時候該用read().decode('utf-8')可以顯示正常結果啦。
'''
[b'0\t\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x90\x97\xef\xbc\x9f\xe7\xab\x8b\xe5\xa4\x8f\xe4\xba\x86\xef\xbc\x8c\xe5\xbf\x83\xe4\xb8\xad\xe5\x8f\x88\xe6\x8a\x8a\xe4\xbd\xa0\xe6\x83\xb3\xe8\xb5\xb7\xe3\x80\x82\xe6\x84\xbf\xe4\xbc\x9a\xe5\xbf\x83\xe7\x9a\x84\xe5\xbe\xae\xe7\xac\x91\xef\xbc\x8c\xe6\x8e\x92\xe6\xbb\xa1\xe4\xbd\xa0\xe6\xaf\x8f\xe5\xa4\xa9\xe7\x9a\x84\xe6\x97\xa5\xe5\x8e\x86\xef\xbc\x9b\xe6\x8a\x9b\xe5\xbc\x83\xe5\x8e\x8b\xe5\x8a\x9b\xef\xbc\x8c\xe5\xa9\xb4\xe5\x84\xbf\xe8\x88\xac\xe6\x83\xac\xe6\x84\x8f\xe5\x9c\xb0\xe5\x91\xbc\xe5\x90\xb8\xef\xbc\x9b\xe5\xa4\xa9\xe6\xb0\x94\xe8\xbd\xac\xe7\x83\xad\xef\xbc\x8c\xe5\x88\xab\xe5\xbf\x98\xe5\xa5\xbd\xe5
CSDN:http://blog.csdn.net/kicilove/article/
github:https://github.com/zhaohuicici?tab=repositories