1

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.

1
  • One thing you should make clear for better answers: do you want to replace all occurrences of "ATOM" in your file (as suggested by your question text) or only those that appear at the beginning of the line (as suggested by your code)? Commented Jun 22, 2012 at 22:55

3 Answers 3

6

The problem is that there's no comma after ATOM in the input, so word[0] doesn't equal ATOM. You should be splitting on spaces, not commas.

You could also just use split() without arguments.

Since you only do output.write when a match is found, the output stays empty.

P.S. Try to use with statements when opening files:

with open('SECOND_orbitsJ22.txt','r') as input,
    open('SECOND_orbitsJ22_out.txt','w') as output:
   ...

Also, Alexander suggests the right tool for replacement: str.replace. However, use it with caution as it's not position-aware. re.sub is a more flexible alternative.

Sign up to request clarification or add additional context in comments.

Comments

4

Use replace.

line.replace("ATOM", "1").replace("# ORB", " ")

Untested code:

input  = open('inp.txt', 'r')
output = open('out.txt', 'w')
clean  = input.read().replace("ATOM", "1").replace("# ORB", " ")
output.write(clean)

Working example.

6 Comments

Where would I put line.replace("ATOM","1") in the code I posted? Or would it be a different code? Sorry I am new to python.
input = open(file); output = input.read().replace("ATOM",1) should replace all occurrances in file ... you dont need to evaluate each line
It didn't change anything in the outfile.
@JamdeJam, I can see it working here (without reading from a file though)
@ Alexander..Thank you so much! It works great! Now I am going to try and create a comma delimted file with this..(Hopefully I can do this). Thanks again!
|
1

Based on the file segment you've pasted here, you'll want to split each line on a space, rather than a comma. If there are no commas, line.split(',') has no effect, and word[0] is empty. Your output file is empty because you are never writing to it, as ATOM will never be equal to the empty string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.