0

I have a large file in which some sections appear as follows.

ATOM      1  N   GLY A   8      10.001   3.812  45.690  1.00 44.05           N  
ATOM      2  CA  GLY A   8       9.207   4.845  44.955  1.00 42.92           C  
ATOM      3  C   GLY A   8       9.481   4.660  43.462  1.00 42.00           C  

I need a python code that replaces the values in indexes 56-59 ("1.00" s) with "0.00". Similarly, the code replaces values in indexes 61-65 (44.05, 42.92, and 42.00) with "0.00". I expect an output as follows. Is there anyone who can assist me?

ATOM      1  N   GLY A   8      10.001   3.812  45.690  0.00  0.00           N  
ATOM      2  CA  GLY A   8       9.207   4.845  44.955  0.00  0.00           C  
ATOM      3  C   GLY A   8       9.481   4.660  43.462  0.00  0.00           C  

Here is the primary code I use.

# Open the file
pdb_text = open("b.txt","r")
# Read contents of the pdb file to string
#rline = ""
lines=pdb_text.readlines()
for line in lines:
    rline = line.replace("1.00","0.00")
    print(rline)
  • 1
    Can you share any code you have tried so far? – dosas Aug 07 '22 at 09:25
  • Here is the primary code I use. # Open the file pdb_text = open("b.txt","r") # Read contents of the pdb file to string #rline = "" lines=pdb_text.readlines() for line in lines: rline = line.replace("1.00","0.00") print(rline) – Mehdi Irani Aug 07 '22 at 09:52
  • You should parse the lines into a more suitable data structure before changing the values. See for example https://stackoverflow.com/questions/4914008/how-to-efficiently-parse-fixed-width-files. Then write them out again. – mkrieger1 Aug 07 '22 at 10:06

2 Answers2

0

Look into text slicing with array notation [:] Strings start from 0 so you have to adjust the indexes. With the data you gave int data.txt this should work.

# Open the file
pdb_text = open("data.txt","r")
# Read contents of the pdb file to string
#rline = ""
lines=pdb_text.readlines()
for line in lines:
    rline = line[0:56]+"0.00"+ "  0.00"+ line[66:]
#    print(line)  #debug
    print(rline,end="")
0

You can work it by regex

import re

"""
with open("b.txt","r") as f:
    text = f.read()
"""

text = """
ATOM      1  N   GLY A   8      10.001   3.812  45.690  1.00 44.05           N
ATOM      2  CA  GLY A   8       9.207   4.845  44.955  1.00 42.92           C
ATOM      3  C   GLY A   8       9.481   4.660  43.462  1.00 42.00           C
""".strip()

lines = text.split('\n')

def replace(matched):
    return "0.00" if matched.start() in (56, 61) else matched.group()

regex = re.compile(r'\S+')

for i, line in enumerate(lines):
    lines[i] = regex.sub(replace, line)

new_text = '\n'.join(lines)
print(new_text)
Jason Yang
  • 11,284
  • 2
  • 9
  • 23