[Python Glasgow] How to write lines that start with TRAN to another output file

Dougal Matthews dougal85 at gmail.com
Mon Mar 25 13:40:30 CET 2013


You can also use relative paths "data/something.txt" will access the files in the data directory relative to the python file you run. 

To create files, you need to look at the different file opening modes. They are described here: http://docs.python.org/2/library/functions.html#open - currently you are using one mode "a" for your output - you might want to choose one of the others.

Dougal


On Monday, 25 March 2013 at 12:36, Hedieh Ebrahimi wrote:

> Thanks Dougal, but one question, the full path  works if I am working on this computer and with this user.
> 
> Is there anyway I could set this if i am planning to run the code on another system?
> 
> Also I am getting output to an output.txt file which I have already created. 
> Is there anyway to ask python to create a text file ?
> 
> I checked python File I\O but I couldnt find anything on creating files.
> 
> Id appreciate any help
> 
> Thanks
> 
> 
> 
> On 25 March 2013 12:32, Dougal Matthews <dougal85 at gmail.com (mailto:dougal85 at gmail.com)> wrote:
> > You just need to use the full path to the file - for example; 
> > 
> > open('/path/to/file/p.txt')
> > 
> > If your on windows that will likely that with c:// etc.
> > 
> > 
> > On Monday, 25 March 2013 at 11:28, Hedieh Ebrahimi wrote:
> > 
> > > Hi all, 
> > > 
> > > I am getting an input file and then im writing to an output file. 
> > > 
> > > Right now they are both in the current working directory. 
> > > 
> > > If I want to get the file from a separate directory and write the output to a separate directory not the same as the working directory, what should i do?
> > > 
> > > Thanks for help
> > > 
> > > Hedie
> > > 
> > > 
> > > On 21 March 2013 18:01, Hedieh Ebrahimi <hedieh.ebrahimi at amphos21.com (mailto:hedieh.ebrahimi at amphos21.com)> wrote:
> > > > #! /usr/bin/python
> > > > 
> > > > # Python Program to read a text file, edit it and give a text file as output
> > > > 
> > > > 
> > > > 
> > > > #print("Python is really a great language,", "isnt it?");
> > > > #searchquery='TRAN'
> > > > 
> > > > 
> > > > 
> > > > 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())
> > > >         ###columns = line.split()
> > > >         
> > > > #with open('Output.txt', 'a') as f2:
> > > > #f=open('Output.txt')
> > > > with open('Output.txt') as f2:
> > > >   with open('Output2.txt') as f3:
> > > >     s=f2.readlines()
> > > >     str1 = ''.join(s)
> > > >     str1.split()
> > > >     print(str1)
> > > >     #f3.write(str1)
> > > > #print('Split this Sentence'.split())
> > > > #with open('Output2.txt') as f3:
> > > > 
> > > > 
> > > > 
> > > > On 21 March 2013 15:44, Hedieh Ebrahimi <hedieh.ebrahimi at amphos21.com (mailto:hedieh.ebrahimi at amphos21.com)> wrote:
> > > > > 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
> > > > > 
> > > > > 
> > > > > 
> > > > > On 21 March 2013 14:24, James Boyd <james.boyd at hotmail.co.uk (mailto:james.boyd at hotmail.co.uk)> wrote:
> > > > > > Hi Heidi,
> > > > > > 
> > > > > > Perhaps check your indentation, the code under the 'if' does not look indented correctly on my phone.  Also, assumption is that the p.txt file has some content where lines start with TRAN and the file/directory permissions are correct.
> > > > > > 
> > > > > > I tried the code and works fine for me
> > > > > > 
> > > > > > James
> > > > > > 
> > > > > > On 21 Mar 2013, at 13:00, "Hedieh Ebrahimi" <hedieh.ebrahimi at amphos21.com (mailto:hedieh.ebrahimi at amphos21.com)> 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
> > > > > > > _______________________________________________
> > > > > > > Glasgow mailing list
> > > > > > > Glasgow at python.org (mailto:Glasgow at python.org)
> > > > > > > http://mail.python.org/mailman/listinfo/glasgow
> > > > > > _______________________________________________
> > > > > > Glasgow mailing list
> > > > > > Glasgow at python.org (mailto:Glasgow at python.org)
> > > > > > http://mail.python.org/mailman/listinfo/glasgow
> > > > > 
> > > > 
> > > 
> > > _______________________________________________
> > > Glasgow mailing list
> > > Glasgow at python.org (mailto:Glasgow at python.org)
> > > http://mail.python.org/mailman/listinfo/glasgow
> > > 
> > > 
> > > 
> > 
> > 
> > 
> > _______________________________________________
> > Glasgow mailing list
> > Glasgow at python.org (mailto:Glasgow at python.org)
> > http://mail.python.org/mailman/listinfo/glasgow
> > 
> 
> _______________________________________________
> Glasgow mailing list
> Glasgow at python.org (mailto:Glasgow at python.org)
> http://mail.python.org/mailman/listinfo/glasgow
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/glasgow/attachments/20130325/6eae39a9/attachment-0001.html>


More information about the Glasgow mailing list