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