tags:

views:

1559

answers:

6

hi,

I have a very basic problem. I am learning my first steps with python & scripting in general and so even this makes me wonder:

I want to read & write lines to new file:

ifile=open("C:\Python24\OtsakkeillaSPSS.csv", "r")
ofile = open("C:\Python24\OtsakkeillaSPSSout.csv", "w")

#read first line with headers
line1 = ifile.readline()
print line1

#read following lines which contain data & write it to ofile
for line in ifile:
    if not line:
        break
    ofile.write(line)

if i print this to the screen i get all my lines done nicely:

0,26100,568,8636
0,11130,555,**3570
0,57100,77,2405**
0,60120,116,1193
0,33540,166,5007
0,95420,318,2310
0,20320,560,7607
0,4300,692,3969
0,65610,341,2073
0,1720,0,0
0,78850,228,1515
0,78870,118,1222

If i write it to ofile i end up missing some 15 lines:

0,6100,948,8332
0,26100,568,8636
0,11130,555

I would appreciate if someone could point out to me what is it that i don´t understand?

Reg,

Jaani

+4  A: 
#read following lines which contain data & write it to ofile
for line in ifile:
    if not line:
        continue          #break stops the loop, you should use continue
    ofile.write(line)
SilentGhost
pass should be continue!
stefanw
thanks stefanw.
SilentGhost
+4  A: 

You should be calling ofile.close() according to python docs.

I'm not sure that writes are fully flushed out to a file without an explicit close.

Also, as SilentGhost mentioned, check for empty lines in your input file.

And as mentioned by stefanw below, that "if.. break" statement isn't necessary in a for in.

Tim Hoolihan
Well, this was fast.Ofile.close really did it for some reason i do not yet know but sure will find out.Thank you guys!!Jaani
Jaani
if that did it, please mark as the answer. the reason it worked is that it flushed out the write to the file. writes are usually done in memory and written out when the buffer gets so big, or close is called.
Tim Hoolihan
if `.close` did it, you're probably are not telling us the full story.
SilentGhost
A: 

oh, why does it look so foolish, the formation i mean? Give it another go...

hi,

I have a very basic problem. I am learning my first steps with python & scripting in general and so even this makes me wonder:

I want to read & write lines to new file:

ifile=open("C:\Python24\OtsakkeillaSPSS.csv", "r") ofile = open("C:\Python24\OtsakkeillaSPSSout.csv", "w")

read first line with headers line1 = ifile.readline() print line1 read following lines which contain data & write it to ofile

for line in ifile: if not line: break ofile.write(line)

if i print this to the screen i get all my lines done nicely:

0,26100,568,8636 0,11130,555,3570 0,57100,77,2405 0,60120,116,1193 0,33540,166,5007 0,95420,318,2310 0,20320,560,7607 0,4300,692,3969 0,65610,341,2073 0,1720,0,0 0,78850,228,1515 0,78870,118,1222

If i write it to ofile i end up missing some 15 lines:

0,6100,948,8332 0,26100,568,8636 0,11130,555

I would appreciate if someone could point out to me what is it that i don´t understand?

Reg,

Jaani

Jaani
Please just edit this into your question, instead of making an answer for it.
Ian P
and delete this answer
balpha
+2  A: 

The "if not line:" - Check is unnecessary.

for line in ifile:
    ofile.write(line)
stefanw
A: 

Thanks,

all for all you guys,

problem solved with ofile.close

Jaani
please delete this non-answer
SilentGhost
Please stop spamming. Post a comment, not an answer, if you want to reply to someone's answer.
Ian P
A: 

Why are you using python2.4? the latest is python2.6

and then you can use

from contextlib import nested
ipath = "C:\Python24\OtsakkeillaSPSS.csv"
opath = "C:\Python24\OtsakkeillaSPSSout.csv"
with nested(open(ipath,'r'), open(opath,'w') as ifile, ofile:

    #read first line with headers
    line1 = ifile.readline()
    print line1

    #read following lines which contain data & write it to ofile
    for line in ifile:
        ofile.write(line)
fabrizioM