From hedieh.ebrahimi at amphos21.com Thu Mar 21 13:23:00 2013 From: hedieh.ebrahimi at amphos21.com (Hedieh Ebrahimi) Date: Thu, 21 Mar 2013 13:23:00 +0100 Subject: [python-advocacy] Nothing prints or writes to the output.txt Message-ID: Hi all, I am trying to write the below code to be able to read from a text file and fine lines that start with TRAN and then write this line and the next 2 following lines to another file called Output.txt. my code is as below: with open('p.txt') as f1: with open('Output.txt', 'a') as f2: for line in f1: if line.startswith('TRAN'): print("true") f2.write(line) f2.write(f1.next()) f2.write(f1.next()) but when i type python he.py in folder where the p.txt and Output.txt are located, nothing happens and no data is written to the output file. Could somebody please tell me where is my error? Thanks in advance Heidi -------------- next part -------------- An HTML attachment was scrubbed... URL: From hedieh.ebrahimi at amphos21.com Thu Mar 21 15:54:29 2013 From: hedieh.ebrahimi at amphos21.com (Hedieh Ebrahimi) Date: Thu, 21 Mar 2013 15:54:29 +0100 Subject: [python-advocacy] (no subject) Message-ID: Thanks guys for help with previous question. its working now. I have another question. I need to extract information. so i now have a file like this : TRAN 1 Time= 3.17098E-08 Dt= 3.17098E-08 [y] snes_conv_reason: 3 newton = 36 [ 36] linear = 36 [ 36] cuts = 0 [ 0] --> max chng: dcmx= 5.0315E-08 dc/dt= 5.0315E-08 [mol/s] TRAN 2 Time= 6.34196E-08 Dt= 3.17098E-08 [y] snes_conv_reason: 3 newton = 4 [ 40] linear = 4 [ 40] cuts = 0 [ 0] --> max chng: dcmx= 3.0785E-08 dc/dt= 3.0785E-08 [mol/s] TRAN 3 Time= 1.26839E-07 Dt= 6.34196E-08 [y] snes_conv_reason: 3 newton = 4 [ 44] linear = 4 [ 44] cuts = 0 [ 0] --> max chng: dcmx= 3.5161E-08 dc/dt= 1.7580E-08 [mol/s] -------------------------------------------------------------------------------------- TRAN 1 Time= 3.17098E-08 Dt= 3.17098E-08 [y] snes_conv_reason: 3 newton = 36 [ 36] linear = 36 [ 36] cuts = 0 [ 0] --> max chng: dcmx= 5.0315E-08 dc/dt= 5.0315E-08 [mol/s] In this file i need the following information to be extracted in each consequetive 3 lines. Could you please let me know how I can do this. At the end i want to have a text file at the output producing this: 1 Time 3.17098E-08 Dt 3.17098E-08 newton 36 linear 36 cuts 0 Id appreciate any hints, ive just started python 2 days ago ))) thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Sun Mar 24 14:42:01 2013 From: aahz at pythoncraft.com (Aahz) Date: Sun, 24 Mar 2013 06:42:01 -0700 Subject: [python-advocacy] Nothing prints or writes to the output.txt In-Reply-To: References: Message-ID: <20130324134201.GB28892@panix.com> BTW, in case nobody else told you, the advocacy list is not a good place for getting help. On Thu, Mar 21, 2013, Hedieh Ebrahimi wrote: > > Hi all, > > I am trying to write the below code to be able to read from a text file and > fine lines that start with > TRAN and then write this line and the next 2 following lines to another > file called Output.txt. > > my code is as below: > > > with open('p.txt') as f1: > with open('Output.txt', 'a') as f2: > for line in f1: > if line.startswith('TRAN'): > print("true") > f2.write(line) > f2.write(f1.next()) > f2.write(f1.next()) > > but when i type python he.py in folder where the p.txt and Output.txt are > located, nothing happens and no data is written to the output file. > > Could somebody please tell me where is my error? > > Thanks in advance > Heidi > _______________________________________________ > Advocacy mailing list > Advocacy at python.org > http://mail.python.org/mailman/listinfo/advocacy -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer