[Tutor] Windows problem with large(?) files

2006-02-02 Thread johan nilsson
Dear group,I have a few files that contains data in IEEE 754 format. I need to transform them into a csv file, to be able to read these files in another program. Each file has 3 samples of I and Q. I did develop the attached script. I am sure not the best, but it worked at home on my Linux installation Python 
2.4. I was then very confident that it would work also on my work pc (XP with python 2.3). It does not, I get the following error Traceback (most recent call last): File C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py, line 310, in RunScript
 exec codeObject in __main__.__dict__ File E:\OFDMA_test\iqw.py, line 16, in ? x[i/4]=unpack('f',a[i:i+4])[0]error: unpack str size does not match formatapparently the XP box thinks the file is alot shorter, and that can't be as it is the same file from the same media (USB stick). What is going wrong here?
/Johanimport osfrom struct import *from scipy import *f=file(c:\\tmp\\FSL_good_SNR\\Last_IQ.iqw,'r')a=f.read()f.close()x=zeros(len(a)/4)+0.0
for i in range (0, len(a),4): x[i/4]=unpack('f',a[i:i+4])[0]y=x[0:len(x)/2-1]+1j*x[len(x)/2:len(x)-1]writer = file(c:\\tmp\\FSL_good_SNR\\IQ.csv, 'w')for item in y: writer.write
(repr(real(item))+','+repr(imag(item))+'\n')writer.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Windows problem with large(?) files

2006-02-02 Thread Danny Yoo


On Thu, 2 Feb 2006, johan nilsson wrote:


 apparently the XP box thinks the file is alot shorter, and that can't be
 as it is the same file from the same media (USB stick). What is going
 wrong here?

Hi Johan,

Is it possible that you need to treat the file as a binary file?  It's
rare, but very possible that something like:

f = open(somefilename,'r')
a = f.read()

will give you different results on different platforms, because newline
translation occurs on the Windows end of things.  (\n -- \r\n or visa
versa)  So if your file coincidently has bytes with the sequential values:

##
 ord('\r'), ord('\n')
(13, 10)
##

then we should expect to see those two bytes collapsed down to a single
one through the mechanism of newline translation.


But if this is happening, there's an easy fix:

f = open(somefilename,'rb')
a = f.read()

where we open the file in binary mode.


Good luck!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Windows problem with large(?) files

2006-02-02 Thread johan nilsson
Thanks Danny!that solved it.JohanOn 2/2/06, Danny Yoo [EMAIL PROTECTED] wrote:
On Thu, 2 Feb 2006, johan nilsson wrote: apparently the XP box thinks the file is alot shorter, and that can't be
 as it is the same file from the same media (USB stick). What is going wrong here?Hi Johan,Is it possible that you need to treat the file as a binary file?It'srare, but very possible that something like:
f = open(somefilename,'r')a = f.read()will give you different results on different platforms, because newlinetranslation occurs on the Windows end of things.(\n -- \r\n or visa
versa)So if your file coincidently has bytes with the sequential values:## ord('\r'), ord('\n')(13, 10)##then we should expect to see those two bytes collapsed down to a single
one through the mechanism of newline translation.But if this is happening, there's an easy fix:f = open(somefilename,'rb')a = f.read()where we open the file in binary mode.
Good luck!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor