如何使用 python 替换文本文件中字段中的值
How to use python to replace a value in a field in a text file
我有一个如下所示的文件:
;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 2.9310E-01 1.9173E-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999
我正在尝试使用 python 更改 [ atomtypes ]
标题下倒数第二个和最后一个字段中的值。交易是我 运行 迭代更新此文件的代码,因此我特别希望该字段成为目标,而不是正则表达式“2.931E-01”或“1.9173E-01”。我知道我们可以使用 awk 之类的东西,但我想知道 python 本身是否可行。
这是我目前所做的:
flag = 0
with open("file.itp") as f:
for line in f:
iterable = line.strip().split()
if flag:
line.strip().split()[5] = sigma
line.strip().split()[6]) = eps
print("epsilon is {} and sigma is {}".format(eps, sigma))
if "atomtypes" in iterable:
flag = 1
else:
flag = 0
f.close()
我正在更改 python 中的值,但我无法将该更改发送到文件本身。我该如何解决这个问题?
如有任何建议,我们将不胜感激!
在行上使用枚举以便我们可以访问当前行和下一行
代码
def update_line(filenm, eps, sigma):
with open(filenm, 'r') as fin:
lines = fin.readlines()
for idx, line in enumerate(lines):
if "atomtypes" in line:
# Current line has marker, so change next line (i.e. index idx + 1)
# Split next line into fields
# split from end so so only have three fields, namely:
# 1) everything before next to last field,
# 2) next to last field,
# 3) last field)
arr = lines[idx+1].rsplit(' ', maxsplit = 3)
arr[-3], arr[-1] = f"{eps:e}", f"{sigma:e}"
lines[idx+1] = ' '.join(arr) + "\n" # last_val dropped the '\n' so add back
break
with open(filenm, 'w') as fout:
# Updated lines placed back into file
fout.writelines(lines)
测试
update_line('test.txt', 4.573133E-02, 8.2737123E-01)
文件test.txt之前:
;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 2.9310E-01 1.9173E-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999
在
之后文件test.txt
;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 4.573133e-02 8.273712e-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999
我有一个如下所示的文件:
;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 2.9310E-01 1.9173E-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999
我正在尝试使用 python 更改 [ atomtypes ]
标题下倒数第二个和最后一个字段中的值。交易是我 运行 迭代更新此文件的代码,因此我特别希望该字段成为目标,而不是正则表达式“2.931E-01”或“1.9173E-01”。我知道我们可以使用 awk 之类的东西,但我想知道 python 本身是否可行。
这是我目前所做的:
flag = 0
with open("file.itp") as f:
for line in f:
iterable = line.strip().split()
if flag:
line.strip().split()[5] = sigma
line.strip().split()[6]) = eps
print("epsilon is {} and sigma is {}".format(eps, sigma))
if "atomtypes" in iterable:
flag = 1
else:
flag = 0
f.close()
我正在更改 python 中的值,但我无法将该更改发送到文件本身。我该如何解决这个问题?
如有任何建议,我们将不胜感激!
在行上使用枚举以便我们可以访问当前行和下一行
代码
def update_line(filenm, eps, sigma):
with open(filenm, 'r') as fin:
lines = fin.readlines()
for idx, line in enumerate(lines):
if "atomtypes" in line:
# Current line has marker, so change next line (i.e. index idx + 1)
# Split next line into fields
# split from end so so only have three fields, namely:
# 1) everything before next to last field,
# 2) next to last field,
# 3) last field)
arr = lines[idx+1].rsplit(' ', maxsplit = 3)
arr[-3], arr[-1] = f"{eps:e}", f"{sigma:e}"
lines[idx+1] = ' '.join(arr) + "\n" # last_val dropped the '\n' so add back
break
with open(filenm, 'w') as fout:
# Updated lines placed back into file
fout.writelines(lines)
测试
update_line('test.txt', 4.573133E-02, 8.2737123E-01)
文件test.txt之前:
;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 2.9310E-01 1.9173E-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999
在
之后文件test.txt;
[ atomtypes ]
opls_BZCG BCG1 78.113999 0.000 A 4.573133e-02 8.273712e-01
[ moleculetype ]
; Name nrexcl
BZCG 1
[ atoms ]
; nr type resnr residue atom cgnr charge mass
1 opls_BZCG 1 BZCG BCG1 1 0 78.113999