473,239 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

readlines() reading incorrect number of lines?

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
Dec 20 '07 #1
7 4035
On Dec 21, 6:48 am, Wojciech Gryc <wojci...@gmail.comwrote:
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'.
Dec 20 '07 #2
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

On Dec 20, 3:30 pm, John Machin <sjmac...@lexicon.netwrote:
On Dec 21, 6:48 am, Wojciech Gryc <wojci...@gmail.comwrote:
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'.
Dec 20 '07 #3
On Dec 21, 7:41 am, Wojciech Gryc <wojci...@gmail.comwrote:
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
Dec 20 '07 #4
[Fixing top-posting.]

On Thu, 20 Dec 2007 12:41:44 -0800, Wojciech Gryc wrote:
On Dec 20, 3:30 pm, John Machin <sjmac...@lexicon.netwrote:
[snip]
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.
....
>One possibility: you are running this on Windows and the file contains
Ctrl-Z aka chr(26) aka '\x1a'.

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.

--
Steven
Dec 20 '07 #5
On Dec 21, 8:13 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
[Fixing top-posting.]

On Thu, 20 Dec 2007 12:41:44 -0800, Wojciech Gryc wrote:
On Dec 20, 3:30 pm, John Machin <sjmac...@lexicon.netwrote:
[snip]
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.
...
One possibility: you are running this on Windows and the file contains
Ctrl-Z aka chr(26) aka '\x1a'.
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.

--
Steven
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.
Dec 20 '07 #6
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

On Dec 20, 5:47 pm, John Machin <sjmac...@lexicon.netwrote:
On Dec 21, 8:13 am, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.auwrote:
[Fixing top-posting.]
On Thu, 20 Dec 2007 12:41:44 -0800, Wojciech Gryc wrote:
On Dec 20, 3:30 pm, John Machin <sjmac...@lexicon.netwrote:
[snip]
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.
...
>One possibility: you are running this on Windows and the file contains
>Ctrl-Z aka chr(26) aka '\x1a'.
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.
--
Steven

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.
Dec 21 '07 #7
En Fri, 21 Dec 2007 16:42:21 -0300, Gerry <ge**********@gmail.com>
escribió:
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.

--
Gabriel Genellina

Dec 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard | last post by:
Hi, Can anyone tell me what the difference is between for line in file.readlines( ): and for line in file:
2
by: Yong Wang | last post by:
Hi, I use readlines() to read one data file. Python automatically parses the read contents into a list of lines. When I used list to print out the 1st line, it is ok. When I use the list index 2...
3
by: Jeremy | last post by:
I have a most aggravating problem. I don't understand what is causing readlines() not to read all the lines in the file. I have the following syntax: # some initial stuff XS =...
2
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
2
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
7
by: Nikhil | last post by:
Hi, I am reading a file with readlines method of the filepointer object returned by the open function. Along with reading the lines, I also need to know which line number of the file is read in...
5
by: zxo102 | last post by:
Hello All, I have a system. An instrument attched to 'com1' is wireless connected to many sensors at different locations. The instrument can forward the "commands" (from pyserial's write()) to...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.