解决输入文件数据中的最大值和最小值的问题
我是 python 新手,我需要一些有关名为 search_max.py 的 python 脚本的帮助。
它打开一个“xyz”格式的文件,然后搜索每个坐标的最小值和最大值。问题是当我对 awk 脚本执行相同操作时,我没有得到相同的结果!
我想知道是否数据类型或字符串操作有问题或者...有人可以帮我解决这个问题吗?
Python 脚本:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
inputfile = "peamorphe.xyz"
outputfile = "result.txt"
# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
lineno = lineno + 1
# extraction and strip of data spaced by " "
data = ligne.rstrip('\n\r').split(" ")
# Conditions for min and max
if data[1] < xmin:
xmin = data[1]
wclxmin = lineno
if data[1] > xmax:
xmax = data[1]
wclxmax = lineno
if data[2] < ymin:
ymin = data[2]
wclymin = lineno
if data[2] > ymax:
ymax = data[2]
wclymax = lineno
if data[3] < zmin:
zmin = data[3]
wclzmin = lineno
if data[3] > zmax:
zmax = data[3]
wclzmax = lineno
# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)
# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
lineno = lineno + 1
# extraction and strip of data spaced by " "
data = ligne.rstrip('\n\r').split(" ")
# Conditions for min and max
if data[1] < xmin:
xmin = data[1]
wclxmin = lineno
if data[1] > xmax:
xmax = data[1]
wclxmax = lineno
if data[2] < ymin:
ymin = data[2]
wclymin = lineno
if data[2] > ymax:
ymax = data[2]
wclymax = lineno
if data[3] < zmin:
zmin = data[3]
wclzmin = lineno
if data[3] > zmax:
zmax = data[3]
wclzmax = lineno
# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)
awk 脚本:
#!/usr/bin/awk -f
# from a xyz file
BEGIN{
xmax;xmin;
zmax;zmin;
ymax;ymin;
xcent;ycent;zcent;
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print "At the start of the script";
print "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;
print "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent;
print "";
}
{
if (xmax<$2) xmax = $2
if (xmin>$2) xmin = $2
if (ymax<$3) ymax = $3
if (ymin>$3) ymin = $3
if (zmax<$4) zmax = $4
if (zmin>$4) zmin = $4
}
END{
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print "At the end of the script";
print "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;
print "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent}
您可以在此处下载我的输入文件(14 天): peamorphe.xyz
预先感谢您, 埃希利安。
I am new with python and i need a little help with my python script named search_max.py.
It opens a file "xyz" format and then search for the min and max of each coord. The problem is when i do the same with an awk script i don't get the same resuts!!!
I wonder if there is a problem with the type of data or string operation or ... Can anyone help me solve this problem?
Python script :
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
inputfile = "peamorphe.xyz"
outputfile = "result.txt"
# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
lineno = lineno + 1
# extraction and strip of data spaced by " "
data = ligne.rstrip('\n\r').split(" ")
# Conditions for min and max
if data[1] < xmin:
xmin = data[1]
wclxmin = lineno
if data[1] > xmax:
xmax = data[1]
wclxmax = lineno
if data[2] < ymin:
ymin = data[2]
wclymin = lineno
if data[2] > ymax:
ymax = data[2]
wclymax = lineno
if data[3] < zmin:
zmin = data[3]
wclzmin = lineno
if data[3] > zmax:
zmax = data[3]
wclzmax = lineno
# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)
# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
lineno = lineno + 1
# extraction and strip of data spaced by " "
data = ligne.rstrip('\n\r').split(" ")
# Conditions for min and max
if data[1] < xmin:
xmin = data[1]
wclxmin = lineno
if data[1] > xmax:
xmax = data[1]
wclxmax = lineno
if data[2] < ymin:
ymin = data[2]
wclymin = lineno
if data[2] > ymax:
ymax = data[2]
wclymax = lineno
if data[3] < zmin:
zmin = data[3]
wclzmin = lineno
if data[3] > zmax:
zmax = data[3]
wclzmax = lineno
# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)
awk script :
#!/usr/bin/awk -f
# from a xyz file
BEGIN{
xmax;xmin;
zmax;zmin;
ymax;ymin;
xcent;ycent;zcent;
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print "At the start of the script";
print "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;
print "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent;
print "";
}
{
if (xmax<$2) xmax = $2
if (xmin>$2) xmin = $2
if (ymax<$3) ymax = $3
if (ymin>$3) ymin = $3
if (zmax<$4) zmax = $4
if (zmin>$4) zmin = $4
}
END{
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print "At the end of the script";
print "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;
print "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent}
you can download my input file here (14 days): peamorphe.xyz
Thank you in advance,
Exilien.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
eumiro 在上面的评论中给出了您的代码无法按预期工作的原因。
不过,在 Python 中,有一种更简单的方法:使用 NumPy。文件每列的最大值和最小值的示例代码如下:
这几行代码完成脚本执行的所有操作,包括解析输入文件。如果您还需要最大值和最小值的索引,可以使用
a.argmax()
和a.argmin()
。看起来比较容易,你不觉得吗?
The reason why your code does not work as expected is given by eumiro in his above comment.
There is an far easier approach for this in Python though: Use NumPy. Example code for maxima and minima of each column of your file would be
These few lines of code do everything your script does, including parsing the input file. If you also need the indices of the maxima and minima, you can use
a.argmax()
anda.argmin()
.Looks quite a bit easier, don't you think?