file.read problem

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Feb 17 04:50:48 EST 2006


On Fri, 17 Feb 2006 00:15:31 -0800, wscrsurfdude wrote:

> 
> Farshid Lashkari wrote:
>> > I am working on a script to get parts of raw data out of a file, and
>> > the data I read has to be the data written in the file without CR or
>> > LF.
>>
>> So you just want to remove all the linefeeds? This should work then:
>>
>> data = data.replace('\n','')
>>
>> -Farshid
> 
> The problem is if I remove the linefeeds, I also delete readout data if
> it is 0x0A, and I don't want this, because the files I readout has to
> be a part of the original data. Another idea??

Er, have I understood you correctly? You seem to be saying that some
linefeeds are significant data, and some are not, and you want somebody to
tell you how to remove the insignificant "linefeed = end of line"
characters without removing the significant "linefeed = important data"
characters.

That's easy:

from blackmagic import readmymind, dowhatiwant
fp = file("data", "rb")
readmymind()
data = dowhatiwant(fp.read())

You'll need Python 3.0 for the blackmagic module.

*wink*

Seriously, if this is your problem, then you will have no choice but to
carefully analyse the file yourself, looking at each linefeed and tossing
it away if it is insignificant. We can't tell you how to do that, because
we don't know which linefeeds are data and which are not.



> But still my question is why is the:
> f = open('myfile,'r')
> a = f.read(5000)
> working in linux??

Why shouldn't it work in Linux? The question should be, why is it not
working in Windows? (When did "my code is broken" become the excepted
state of affairs, and "my code works" the mystery that needs solving?)

I wonder whether there is a ctrl-Z in your data, and Windows is
interpreting that as end of file.


-- 
Steven.




More information about the Python-list mailing list