Reputation: 19
I try to read a file use Python. Notepad show that there are 200 lines.
f = open('record.txt', 'r')
for line in f.readlines():
print line
Aparrently there is something wrong with line 17, and the read process encounter false EOF. So how to skip fake EOF and read rest lines? thanks.
Line 17 looks like this: Iˌ$hǃɓǃ ɓI˔$hǃɓǃ ɓBɔ+Iʅ뭶DtHăɓǃ ɓBɔ+LΨBDŽ+Ĥhǃɓǃ ɓBɔ+Iʅ뭶DtHăɓǃ ɓBɔ+LΨBDŽ+ĤhLΨC
Upvotes: 0
Views: 192
Reputation: 7480
Looking at your line sample given, you're apparently trying to read binary file in text mode.
You have to use binary mode for reading binary files to avoid different kinds of problems, including misrepresenting line endings, stopping reading prematurely due to EOF control chars, losing information when transcoding to UTF8 and vice versa (as all UTF encodings are lossy), and so on.
Binary mode is enabled by adding "b"
to list of your mode chars:
f = open('record.txt', 'rb')
for line in f.readlines():
print line
Also, for future reference, .readlines()
reads whole file in memory at once, which can be unwanted if file is large. It's generally better to just iterate over file — it's the same as .readlines()
, but lines are read one by one. So, it can be as simple as:
f = open('record.txt', 'rb')
for line in f:
print line
This is, however, inapplicable, if your file might change while reading.
Upvotes: 1