可悲的是,我对python知之甚少,有人帮助我编写脚本。
我需要将xyz文件中的O(指向氧气)更改为Ob(氧气体积)或Ow(氧气水)。
在definitions.txt文件中:
1-258
259-795
1-258 - 这指的是前1-258个原子,如果这些文件中有一个O,那么它应该是Ob 259-795这指的是259-795个原子,如果这些文件中有一个O,那么它应该是Ow
我一直收到此错误
追踪(最近的呼叫最后):
test.py,第47行,in
natoms = int(xyz [0] .split()[0])
ValueError:int()的基数为10的无效文字:'1-258'
任何有关如何解决这个问题的建议都将非常感谢!!!
SCRIPT:
*!/usr/bin/env python*
import numpy as np
from sys import argv,exit
from itertools import chain
from math import sqrt
from collections import defaultdict
def get_coordinates(coordinates):
x = float(coordinates.split()[1])
y = float(coordinates.split()[2])
z = float(coordinates.split()[3])
return x,y,z
*Calculate distance between 3 (x,y,z) points*
def vector_distance((x1,y1,z1,x2,y2,z2)):
dist = sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2-z1)**2)
return dist
def group_to_range(group):
group = ''.join(group.split())
sign, g = ('-', group[1:]) if group.startswith('-') else ('', group)
r = g.split('-', 1)
r[0] = sign + r[0]
r = sorted(int(__) for __ in r)
return range(r[0], 1 + r[-1])
*Expand and sort the list of numbers*
def rangeexpand(txt):
ranges = chain.from_iterable(group_to_range(__) for __ in txt.split(','))
return sorted(set(ranges))
return range(r[0], 1 + r[-1])
*Expand and sort the list of numbers
def rangeexpand(txt):
ranges = chain.from_iterable(group_to_range(__) for __ in txt.split(','))
return sorted(set(ranges))
Usage
create 'defnitions.txt in your directory and add the atom numbers of the
bulk oxygens on the first line and the water oxygens on the second line
e.g
1-45,56,58
46-55,57,59-60*
xyz = open(argv[1],'r').read().splitlines()
definitions=open("definitions.txt",'r').read().splitlines()
bulk=rangeexpand(definitions[0])
water=rangeexpand(definitions[1])
natoms= int(xyz[0].split()[0])
ntrajs=len(xyz)/(natoms+2)
fout=open(argv[1]+"_edited","w")
for i,j in enumerate(xyz):
if int(j.split()[0])==natoms:
fout.write("{0}\n".format(j))
fout.write("{0}\n".format(xyz[i+1]))
if i+1 in bulk:
fout.write("{0}-B {1} {2} {3}".format(j.split()[0],j.split()
[1],j.split()[2],j.split()[3]))
if i+1 in water:
fout.write("{0}-W {1} {2} {3}".format(j.split()[0],j.split()
[1],j.split()[2],j.split()[3]))
答案 0 :(得分:0)
str.split
的默认行为是在空格上拆分。如果要分割另一个字符/字符串(在您的情况下为'-'
),则需要在调用方法时将其显式提供为参数。