在 line.split() 命令中分配的变量不会读入函数
Variables that have been assigned in a line.split() command, are not read into a function
在此代码中,来自 [float(n) for n in line.split()]
命令的变量 pressure
和 enthalpy
未读入函数:
import numpy as np
pressure_gibbs = open('pressure_gibbs_all_points.dat', 'w')
pressure_gibbs.write('#pressure gibbs\n')
## FUNCTIONS:
def G(H,S):
# G = H - TS
# Then:
gibbs = H - 298.15 * S/4.0
return gibbs
with open('entropies_parsed.dat') as entropies_parsed, open('pressure_enthalpy_all_points.dat') as enthalpy_pressure: # open the file
entropies_parsed.next() # skip the first line
enthalpy_pressure.next()
entropy = [float(line) for line in entropies_parsed]
#print entropy
for line in enthalpy_pressure:
#print entropy
pressure, enthalpy = [float(n) for n in line.split()]
#print pressure
#print enthalpy
for i in entropy:
#print i
gibbs = G(enthalpy, i)
#print gibbs
pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs))
pressure_gibbs.close()
此文件夹中只有两个文件是运行此代码所必需的:
pressure_enthalpy_all_points.dat
:
# pressure enthalpy
2 3
5 4
3.5 2
entropies_parsed.dat
:
# entropies
0.5
0.2
0.47
据我所知,这是我所能达到的最好结果,而且缩进位置是正确的。
但是,
这段代码给出一个文件,pressure_gibbs_all_points.dat
:
#pressure gibbs
2.0 -34.26875
2.0 -11.9075
2.0 -32.032625
5.0 -33.26875
5.0 -10.9075
5.0 -31.032625
3.5 -35.26875
3.5 -12.9075
3.5 -33.032625
这是错误的。
如果你能帮助我,我将不胜感激。
你的输出文件显示的值似乎与代码中的数学匹配,所以我唯一能看到的是你有 9 个计算,而你期望的是 3 个。这是因为你有一个嵌套循环,所以你首先循环压力,然后循环熵。所以你在 p = 2.0 时计算 3 个熵值的 Gibbs,然后再次计算 p = 5.0,最后计算 p = 3.5,所以你有 9 次计算。如果您只查找 3 个计算:
for i, line in zip(entropy, enthalpy_pressure):
#print entropy
pressure, enthalpy = [float(n) for n in line.split()]
#print pressure
#print enthalpy
#print i
gibbs = G(enthalpy, i)
#print gibbs
pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs))
我认为是时候深入了解 numpy 以及为什么 numpy 和 python 的组合非常棒了。这段代码可以满足您的需求。这里有很多,所以你只需要花时间消化它。我创建了一个新答案,因为原始答案有关于你的第一个问题的细节,但下面的代码实际上是你应该如何做的。如果出现错误,请确保为分隔符等输入正确的值。
import numpy as np
# read in the data, and tranpose the columns into rows for easy unpacking
entropy = np.loadtxt('entropies_parsed.dat', skiprows=1).T
enthalpy, pressure = np.loadtxt('pressure_enthalpy_all_points.dat', skiprows=1).T
gibbs = enthalpy - 298.15 * entropy / 4.0
# stack the data together into a new, 2-row array, and then transpose back into column format for file writing
output_array = np.vstack((pressure, gibbs)).T
np.savetxt('pressure_gibbs_all_points.dat', output_array, header="pressure\tgibbs", fmt="%0.06g")
在此代码中,来自 [float(n) for n in line.split()]
命令的变量 pressure
和 enthalpy
未读入函数:
import numpy as np
pressure_gibbs = open('pressure_gibbs_all_points.dat', 'w')
pressure_gibbs.write('#pressure gibbs\n')
## FUNCTIONS:
def G(H,S):
# G = H - TS
# Then:
gibbs = H - 298.15 * S/4.0
return gibbs
with open('entropies_parsed.dat') as entropies_parsed, open('pressure_enthalpy_all_points.dat') as enthalpy_pressure: # open the file
entropies_parsed.next() # skip the first line
enthalpy_pressure.next()
entropy = [float(line) for line in entropies_parsed]
#print entropy
for line in enthalpy_pressure:
#print entropy
pressure, enthalpy = [float(n) for n in line.split()]
#print pressure
#print enthalpy
for i in entropy:
#print i
gibbs = G(enthalpy, i)
#print gibbs
pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs))
pressure_gibbs.close()
此文件夹中只有两个文件是运行此代码所必需的:
pressure_enthalpy_all_points.dat
:
# pressure enthalpy
2 3
5 4
3.5 2
entropies_parsed.dat
:
# entropies
0.5
0.2
0.47
据我所知,这是我所能达到的最好结果,而且缩进位置是正确的。
但是,
这段代码给出一个文件,pressure_gibbs_all_points.dat
:
#pressure gibbs
2.0 -34.26875
2.0 -11.9075
2.0 -32.032625
5.0 -33.26875
5.0 -10.9075
5.0 -31.032625
3.5 -35.26875
3.5 -12.9075
3.5 -33.032625
这是错误的。
如果你能帮助我,我将不胜感激。
你的输出文件显示的值似乎与代码中的数学匹配,所以我唯一能看到的是你有 9 个计算,而你期望的是 3 个。这是因为你有一个嵌套循环,所以你首先循环压力,然后循环熵。所以你在 p = 2.0 时计算 3 个熵值的 Gibbs,然后再次计算 p = 5.0,最后计算 p = 3.5,所以你有 9 次计算。如果您只查找 3 个计算:
for i, line in zip(entropy, enthalpy_pressure):
#print entropy
pressure, enthalpy = [float(n) for n in line.split()]
#print pressure
#print enthalpy
#print i
gibbs = G(enthalpy, i)
#print gibbs
pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs))
我认为是时候深入了解 numpy 以及为什么 numpy 和 python 的组合非常棒了。这段代码可以满足您的需求。这里有很多,所以你只需要花时间消化它。我创建了一个新答案,因为原始答案有关于你的第一个问题的细节,但下面的代码实际上是你应该如何做的。如果出现错误,请确保为分隔符等输入正确的值。
import numpy as np
# read in the data, and tranpose the columns into rows for easy unpacking
entropy = np.loadtxt('entropies_parsed.dat', skiprows=1).T
enthalpy, pressure = np.loadtxt('pressure_enthalpy_all_points.dat', skiprows=1).T
gibbs = enthalpy - 298.15 * entropy / 4.0
# stack the data together into a new, 2-row array, and then transpose back into column format for file writing
output_array = np.vstack((pressure, gibbs)).T
np.savetxt('pressure_gibbs_all_points.dat', output_array, header="pressure\tgibbs", fmt="%0.06g")