readlines() reading incorrect number of lines?

W

Wojciech Gryc

Hi,

I'm currently using Python to deal with a fairly large text file (800
MB), which I know has about 85,000 lines of text. I can confirm this
because (1) I built the file myself, and (2) running a basic Java
program to count lines yields a number in that range.

However, when I use Python's various methods -- readline(),
readlines(), or xreadlines() and loop through the lines of the file,
the line program exits at 16,000 lines. No error output or anything --
it seems the end of the loop was reached, and the code was executed
successfully.

I'm baffled and confused, and would be grateful for any advice as to
what I'm doing wrong, or why this may be happening.

Thank you,
Wojciech Gryc
 
J

John Machin

Hi,

I'm currently using Python to deal with a fairly large text file (800
MB), which I know has about 85,000 lines of text. I can confirm this
because (1) I built the file myself, and (2) running a basic Java
program to count lines yields a number in that range.

However, when I use Python's various methods -- readline(),
readlines(), or xreadlines() and loop through the lines of the file,
the line program exits at 16,000 lines. No error output or anything --
it seems the end of the loop was reached, and the code was executed
successfully.

I'm baffled and confused, and would be grateful for any advice as to
what I'm doing wrong, or why this may be happening.

What platform, what version of python?

One possibility: you are running this on Windows and the file contains
Ctrl-Z aka chr(26) aka '\x1a'.
 
W

Wojciech Gryc

Hi,

Python 2.5, on Windows XP. Actually, I think you may be right about
\x1a -- there's a few lines that definitely have some strange
character sequences, so this would make sense... Would you happen to
know how I can actually fix this (e.g. replace the character)? Since
Python doesn't see the rest of the file, I don't even know how to get
to it to fix the problem... Due to the nature of the data I'm working
with, manual editing is also not an option.

Thanks,
Wojciech
 
J

John Machin

Hi,

Python 2.5, on Windows XP. Actually, I think you may be right about
\x1a -- there's a few lines that definitely have some strange
character sequences, so this would make sense... Would you happen to
know how I can actually fix this (e.g. replace the character)? Since
Python doesn't see the rest of the file, I don't even know how to get
to it to fix the problem... Due to the nature of the data I'm working
with, manual editing is also not an option.

Please don't top-post.

Quick hack to remove all occurrences of '\x1a' (untested):

fin = open('old_file', 'rb') # note b BINARY
fout = open('new_file', 'wb')
blksz = 1024 * 1024
while True:
blk = fin.read(blksz)
if not blk: break
fout.write(blk.replace('\x1a', ''))
fout.close()
fin.close()

You may however want to investigate the "strange character sequences"
that have somehow appeared in your file after you built it
yourself :)

HTH,
John
 
S

Steven D'Aprano

[Fixing top-posting.]

Hi,

Python 2.5, on Windows XP. Actually, I think you may be right about \x1a
-- there's a few lines that definitely have some strange character
sequences, so this would make sense... Would you happen to know how I
can actually fix this (e.g. replace the character)? Since Python doesn't
see the rest of the file, I don't even know how to get to it to fix the
problem... Due to the nature of the data I'm working with, manual
editing is also not an option.

Thanks,
Wojciech


Open the file in binary mode:

open(filename, 'rb')


and Windows should do no special handling of Ctrl-Z characters.
 
J

John Machin

[Fixing top-posting.]





Python 2.5, on Windows XP. Actually, I think you may be right about \x1a
-- there's a few lines that definitely have some strange character
sequences, so this would make sense... Would you happen to know how I
can actually fix this (e.g. replace the character)? Since Python doesn't
see the rest of the file, I don't even know how to get to it to fix the
problem... Due to the nature of the data I'm working with, manual
editing is also not an option.
Thanks,
Wojciech

Open the file in binary mode:

open(filename, 'rb')

and Windows should do no special handling of Ctrl-Z characters.

I don't know whether it's a bug or a feature or just a dark corner,
but using mode='rU' does no special handling of Ctrl-Z either.
x = 'foo\r\n\x1abar\r\n'
f = open('udcray.txt', 'wb')
f.write(x)
f.close()
open('udcray.txt', 'r').readlines() ['foo\n']
open('udcray.txt', 'rU').readlines() ['foo\n', '\x1abar\n']
for line in open('udcray.txt', 'rU'):
.... print repr(line)
....
'foo\n'
'\x1abar\n'
Using 'rU' should make the OP's task of finding the strange character
sequences a bit easier -- he won't have to read a block at a time and
worry about the guff straddling a block boundary.
 
G

Gerry

Something I've occasionally found helpful with problem text files is
to build a histogram of character counts, something like this:


"""
chist.py
print a histogram of character frequencies in a nemed input file
"""

import sys

whitespace = ' \t\n\r\v\f'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation

try:
fname = sys.argv[1]
except:
print "usage is chist yourfilename"
sys.exit()

chars = {}

f = open (fname, "rb")
lines = f.readlines()
for line in lines:
for c in line:
try:
chars[ord(c)] += 1
except:
chars[ord(c)] = 1

ords = chars.keys()
ords.sort()

for o in ords:
if chr(o) in printable:
c = chr(o)
else:
c = "UNP"

print "%5d %-5s %10d" % (o, c, chars[o])
print "_" * 50


Gerry



[Fixing top-posting.]
Open the file in binary mode:
open(filename, 'rb')
and Windows should do no special handling of Ctrl-Z characters.

I don't know whether it's a bug or a feature or just a dark corner,
but using mode='rU' does no special handling of Ctrl-Z either.
x = 'foo\r\n\x1abar\r\n'
f = open('udcray.txt', 'wb')
f.write(x)
f.close()
open('udcray.txt', 'r').readlines() ['foo\n']
open('udcray.txt', 'rU').readlines()

['foo\n', '\x1abar\n']>>> for line in open('udcray.txt', 'rU'):

... print repr(line)
...
'foo\n'
'\x1abar\n'



Using 'rU' should make the OP's task of finding the strange character
sequences a bit easier -- he won't have to read a block at a time and
worry about the guff straddling a block boundary.
 
G

Gabriel Genellina

whitespace = ' \t\n\r\v\f'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation

You do know that most -if not all- of those sets are available as
attributes of the string module, don't you?
You could replace all the lines above with: from string import printable,
as it's the only constant used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top