[Python Glasgow] How to write lines that start with TRAN to another output file
Hedieh Ebrahimi
hedieh.ebrahimi at amphos21.com
Thu Mar 21 18:01:27 CET 2013
#! /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>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> 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>
>> 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
>> > http://mail.python.org/mailman/listinfo/glasgow
>> _______________________________________________
>> Glasgow mailing list
>> 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/20130321/6a3e0941/attachment-0001.html>
More information about the Glasgow
mailing list