Replacing a word in a text file with a value using python

Posted by Jamde Jam on Stack Overflow See other posts from Stack Overflow or by Jamde Jam
Published on 2012-06-22T21:14:33Z Indexed on 2012/06/22 21:15 UTC
Read the original article Hit count: 232

Filed under:
|

I have been trying to replace a word in a text file with a value (say 1), but my outfile is blank.I am new to python (its only been a month since I have been learning it).

My file is relatively large, but I just want to replace a word with the value 1 for now. Here is a segment of what the file looks like:

NAME SECOND_1

ATOM 1 6 0 0 0 # ORB 1

ATOM 2 2 0 12/24 0 # ORB 2

ATOM 3 2 12/24 0 0 # ORB 2

ATOM 4 2 0 0 4/24 # ORB 3

ATOM 5 2 0 0 20/24 # ORB 3

ATOM 6 2 0 0 8/24 # ORB 3

ATOM 7 2 0 0 16/24 # ORB 3

ATOM 8 6 0 0 12/24 # ORB 1

ATOM 9 2 12/24 0 12/24 # ORB 2

ATOM 10 2 0 12/24 12/24 # ORB 2

#1

#2

#3

I want to first replace the word ATOM with the value 1. Next I want to replace #ORB with a space. Here is what I am trying thus far.

input = open('SECOND_orbitsJ22.txt','r')
 output=open('SECOND_orbitsJ22_out.txt','w')
for line in input:
    word=line.split(',')
    if(word[0]=='ATOM'):
        word[0]='1'
        output.write(','.join(word))

Can anyone offer any suggestions or help? Thanks so much.

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x