Re: writing on file not until the end

2009-05-25 Thread Peter Otten
Alexzive wrote:

 I am a newby with python. I wrote the following code to extract a text
 from a file and write it to another file:
 
 linestring = open(path, 'r').read() #read all the inp file in
 linestring
 
 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)
 
 unfortunately when I check N.txt some lines are missing (it only crop
 until 57, 0.231749688431, 0.0405121944142 but I espect the final
 line to be 242, 0.2979675, 0.224605896461. I check textN and it has
 all the lines until 242..)
 
 when I try Nfile.write(textN) again it writes some more lines but
 still not all!
 what is wrong whit this?

Do you check the contents of the resulting file by feeding it to another 
program? 

 53, 0.170973146505, 0.0466686190136
 57, 0.231749688431, 0.0405121944142
 t60, 0.250420691759, 0.0399644155193
 58, 0.234883810317, 0.0488399925217
 61, 0.2666025, 0.03541845
 
There's a t at the start of the line after the last line you claim to see, 
so maybe your input file is corrupt and your reader stumbles over that line.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread Alessandro
On May 25, 8:38 am, Peter Otten __pete...@web.de wrote:
 Alexzive wrote:
  I am a newby with python. I wrote the following code to extract a text
  from a file and write it to another file:

  linestring = open(path, 'r').read() #read all the inp file in
  linestring

  i=linestring.index(*NODE)
  i=linestring.index(E,i)
  e=linestring.index(*,i+10)
  textN = linestring[i+2:e-1] # crop the ELement+nodes list
  Nfile = open(N.txt, w)
  Nfile.write(textN)

  unfortunately when I check N.txt some lines are missing (it only crop
  until 57, 0.231749688431, 0.0405121944142 but I espect the final
  line to be 242, 0.2979675, 0.224605896461. I check textN and it has
  all the lines until 242..)

  when I try Nfile.write(textN) again it writes some more lines but
  still not all!
  what is wrong whit this?

 Do you check the contents of the resulting file by feeding it to another
 program?

  53, 0.170973146505, 0.0466686190136
  57, 0.231749688431, 0.0405121944142
  t60, 0.250420691759, 0.0399644155193
  58, 0.234883810317, 0.0488399925217
  61, 0.2666025, 0.03541845

 There's a t at the start of the line after the last line you claim to see,
 so maybe your input file is corrupt and your reader stumbles over that line.

 Peter

Thank you all for replying.

- yes, I oversaw the t at 60, 0.250420691759, 0.0399644155193. I
then removed it for the following tests
- changing from e-1 to e apparently solved the problem, BUT I then
realized it was just because a second access to the file (still open)
seems to be the real solution -- after Nfile.close() e retrying with
e instead of e-1 the problem is still there.
- I also tryied to inizialize e and i to zero -- nothing changes
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:

linestring = open(path, 'r').read()
i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open(N.txt, w)
Nfile.write(textN)

linestring = open(path, 'r').read()
i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open(N.txt, w)
Nfile.write(textN)

Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread Peter Otten
Alessandro wrote:

 - until now, the only solution which works is to repeat the code while
 the file is still open, which means a quite redundant code:
 
 linestring = open(path, 'r').read()
 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)
 
 linestring = open(path, 'r').read()
 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)

Is this the complete script? The only effect of the second copy of your code 
above is that it implicitly closes the first Nfile. Otherwise it is cargo 
cult.

Peter 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread John Machin
On May 25, 6:30 pm, Alessandro zasaconsult...@gmail.com wrote:
 On May 25, 8:38 am, Peter Otten __pete...@web.de wrote:



  Alexzive wrote:
   I am a newby with python. I wrote the following code to extract a text
   from a file and write it to another file:

   linestring = open(path, 'r').read() #read all the inp file in
   linestring

   i=linestring.index(*NODE)
   i=linestring.index(E,i)
   e=linestring.index(*,i+10)
   textN = linestring[i+2:e-1] # crop the ELement+nodes list
   Nfile = open(N.txt, w)
   Nfile.write(textN)

   unfortunately when I check N.txt some lines are missing (it only crop
   until 57, 0.231749688431, 0.0405121944142 but I espect the final
   line to be 242, 0.2979675, 0.224605896461. I check textN and it has
   all the lines until 242..)

   when I try Nfile.write(textN) again it writes some more lines but
   still not all!
   what is wrong whit this?

  Do you check the contents of the resulting file by feeding it to another
  program?

   53, 0.170973146505, 0.0466686190136
   57, 0.231749688431, 0.0405121944142
   t60, 0.250420691759, 0.0399644155193
   58, 0.234883810317, 0.0488399925217
   61, 0.2666025, 0.03541845

  There's a t at the start of the line after the last line you claim to see,
  so maybe your input file is corrupt and your reader stumbles over that line.

  Peter

 Thank you all for replying.

 - yes, I oversaw the t at 60, 0.250420691759, 0.0399644155193. I
 then removed it for the following tests
 - changing from e-1 to e apparently solved the problem, BUT I then
 realized it was just because a second access to the file (still open)
 seems to be the real solution -- after Nfile.close() e retrying with
 e instead of e-1 the problem is still there.
 - I also tryied to inizialize e and i to zero -- nothing changes
 - until now, the only solution which works is to repeat the code while
 the file is still open, which means a quite redundant code:

 linestring = open(path, 'r').read()
 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)

Insert here:
Nfile.close()

Remove the redundant second pass.

 linestring = open(path, 'r').read()
 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread Alessandro
On May 25, 10:58 am, John Machin sjmac...@lexicon.net wrote:
 On May 25, 6:30 pm, Alessandro zasaconsult...@gmail.com wrote:



  On May 25, 8:38 am, Peter Otten __pete...@web.de wrote:

   Alexzive wrote:
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open(N.txt, w)
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until 57, 0.231749688431, 0.0405121944142 but I espect the final
line to be 242, 0.2979675, 0.224605896461. I check textN and it has
all the lines until 242..)

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

   Do you check the contents of the resulting file by feeding it to another
   program?

53, 0.170973146505, 0.0466686190136
57, 0.231749688431, 0.0405121944142
t60, 0.250420691759, 0.0399644155193
58, 0.234883810317, 0.0488399925217
61, 0.2666025, 0.03541845

   There's a t at the start of the line after the last line you claim to 
   see,
   so maybe your input file is corrupt and your reader stumbles over that 
   line.

   Peter

  Thank you all for replying.

  - yes, I oversaw the t at 60, 0.250420691759, 0.0399644155193. I
  then removed it for the following tests
  - changing from e-1 to e apparently solved the problem, BUT I then
  realized it was just because a second access to the file (still open)
  seems to be the real solution -- after Nfile.close() e retrying with
  e instead of e-1 the problem is still there.
  - I also tryied to inizialize e and i to zero -- nothing changes
  - until now, the only solution which works is to repeat the code while
  the file is still open, which means a quite redundant code:

  linestring = open(path, 'r').read()
  i=linestring.index(*NODE)
  i=linestring.index(E,i)
  e=linestring.index(*,i+10)
  textN = linestring[i+2:e-1] # crop the ELement+nodes list
  Nfile = open(N.txt, w)


I closed and restarted the python console. Now this code (with added
Nfile.close() at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1]
Nfile = open(N.txt, w)
Nfile.write(textN)
Nfile.close()

thanks, Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread Dave Angel

Alessandro wrote:

snip

I closed and restarted the python console. Now this code (with added
Nfile.close() at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1]
Nfile = open(N.txt, w)
Nfile.write(textN)
Nfile.close()

thanks, Alex

  
Others had already mentioned close(), but I didn't bother, since it 
would be automatically closed when you exited the function, or finished 
the script.  I never dreamed you were running all this from the 
interpreter.  If so, why didn't you copy the prompts?


The close is best done explicitly, but it is implicit when the Nfile 
goes out of scope, or gets reassigned (like the new open).


However, the bug I described is still there.  Study the following, and 
try it:


def test():
   buf = abcdefghijklmnopqrstg000
   i = buf.index(d)
   e = buf.index(g, i+10)
   buf2 = buf[i+2:e-1]
   print buf2

running it produces the string:

fghijklmnopqrs

Notice the 't' is missing.  If you're deliberately doing that, fine.  
But I doubt it.




--
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-24 Thread Benjamin Kaplan
On Sun, May 24, 2009 at 2:46 PM, Alexzive zasaconsult...@gmail.com wrote:

 Hello,
 I am a newby with python. I wrote the following code to extract a text
 from a file and write it to another file:

 linestring = open(path, 'r').read() #read all the inp file in
 linestring

 i=linestring.index(*NODE)
 i=linestring.index(E,i)
 e=linestring.index(*,i+10)
 textN = linestring[i+2:e-1] # crop the ELement+nodes list
 Nfile = open(N.txt, w)
 Nfile.write(textN)

 unfortunately when I check N.txt some lines are missing (it only crop
 until 57, 0.231749688431, 0.0405121944142 but I espect the final
 line to be 242, 0.2979675, 0.224605896461. I check textN and it has
 all the lines until 242..)

 when I try Nfile.write(textN) again it writes some more lines but
 still not all!
 what is wrong whit this?

 Many thanks for your help!
 Alex

 the original file to crop (in path) looks like this:


[snip file]

I didn't test this, but I would guess you're seeing this because of
buffering. Try adding Nfile.flush() to the end.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-24 Thread Rhodri James
On Sun, 24 May 2009 19:46:01 +0100, Alexzive zasaconsult...@gmail.com  
wrote:



Hello,
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open(N.txt, w)
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until 57, 0.231749688431, 0.0405121944142 but I espect the final
line to be 242, 0.2979675, 0.224605896461. I check textN and it has
all the lines until 242..)

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?


Cutting and pasting (and substituting the sample file for `path`),
it works for me on Python 2.6.2 (Ubuntu Linux).  It must be something
to do with the context you're calling the code in.  Are you checking
the output before the program has finished running or something?

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-24 Thread Dave Angel

Alexzive wrote:

Hello,
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index(*NODE)
i=linestring.index(E,i)
e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open(N.txt, w)
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until 57, 0.231749688431, 0.0405121944142 but I espect the final
line to be 242, 0.2979675, 0.224605896461. I check textN and it has
all the lines until 242..)

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Many thanks for your help!
Alex

the original file to crop (in path) looks like this:

snip


  
Is there a reason you used e-1 instead of e in the slice?  That's going 
to chop off the last newline.  And in some (broken) text viewers, you 
might not see the partial line.  Also, it'll behave a bit different in 
Windows than in one of the Unix variants, due to newline being 
represented by two characters instead of one.


e=linestring.index(*,i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list


--
http://mail.python.org/mailman/listinfo/python-list