python [float(n)for in in line.split()]上只包含一个列的文件



我创建了一个python脚本,我在文件中编写一个 V(卷)的数组:

 import numpy as np
 volume_pressure_energy = open('datafile.dat', 'w') # Open the file, 'w' for writing
 V = np.linspace(62, 72, 5)
 with open('datafile.dat') as volume_pressure_energy:
    np.savetxt('datafile.dat', V, '%10.9f', delimiter='t', header=" volumetpressuretenergy")
 volume_pressure_energy.close()

这将生成此文件datafile.dat

 #  volume       pressure        energy
 62.000000000
 64.500000000
 67.000000000
 69.500000000
 72.000000000

脚本的下一行尝试使用函数和参数来计算pressureenergy

# Parameters
 E0 = -9
 B0 = 7
 V0 = 6
 B0_prime = 4
# Function P(V):
 def P(V): # To use a P(V) is inevitable as the function depends on V
   f0=(3.0/2.0)*B0
   f1=((V0/V)**(7.0/3.0))-((V0/V)**(5.0/3.0))
   f2=((V0/V)**(2.0/3.0))-1
   pressure= f0*f1*(1+(3.0/4.0)*(B0_prime-4)*f2)
   return pressure
# Function E(V):
 def E(V):
   E = E0+ (2.293710449E+17)*(1E-21)*( (9.0/16.0)*(V0*B0) * (  (((V0/V)**   (2.0/3.0)-1)**3)*B0_prime  + ((V0/V)**(2.0/3.0)-1)**2  *  (6.0-4.0*(V0/V)**(2.0/3.0))  ))
   return E

现在,我想阅读datafile.dat并将第一列视为volume数据。此volume数据将输入函数P(V),并将给我pressure。同样,此volume数据将输入函数E(V),并将给我energy

 with open('datafile.dat') as   volume_pressure_energy: # open the file
   volume_pressure_energy.next() # skip the first line
   for line in volume_pressure_energy:
   volume = [float(n) for n in line.split()] # split the lines 
   # (removes linebreaks, tabs and spaces)
   # convert all items to floats.
   pressure = P(volume) # call my function
   energy = E(volume)   # call my function
   volume_pressure_energy.write('{}t{}t{}n'.format(volume, pressure, energy))
 volume_pressure_energy.close()

运行所有此脚本时(下面我发布了完整脚本),它说

 Traceback (most recent call last):
 File "BM-model-Enth-obtention_data_E_vs_P.py", line 76, in <module>
 pressure = P(volume) # call your function
 File "BM-model-Enth-obtention_data_E_vs_P.py", line 50, in P
 f1=((V0/V)**(7.0/3.0))-((V0/V)**(5.0/3.0))
 TypeError: unsupported operand type(s) for /: 'float' and 'list'

显然,功能存在问题。我已经分别运行它们并正常工作,所以问题是Python并不假设datafile.dat的第一列的每一行包含V,即插入函数中的卷。

为什么会发生这种情况?volume = [float(n) for n in line.split()]正在拆分线路并将所有项目转换为浮点部,那么为什么这应该是问题?

完成脚本:

import numpy as np
volume_pressure_energy = open('datafile.dat', 'w') # Open the file, 'w' for writing
V = np.linspace(62, 72, 5)
with open('datafile.dat') as volume_pressure_energy:
   np.savetxt('datafile.dat', V, '%10.9f', delimiter='t', header=" volumetpressuretenergy")
volume_pressure_energy.close()
# Parameters
E0 = -9
B0 = 7
V0 = 6
B0_prime = 4
# Function P(V):
  def P(V): # To use a P(V) is inevitable as the function depends on V
   f0=(3.0/2.0)*B0
   f1=((V0/V)**(7.0/3.0))-((V0/V)**(5.0/3.0))
   f2=((V0/V)**(2.0/3.0))-1
   pressure= f0*f1*(1+(3.0/4.0)*(B0_prime-4)*f2)
   return pressure
# Function E(V):
  def E(V):
   E = E0+ (2.293710449E+17)*(1E-21)*( (9.0/16.0)*(V0*B0) * (  (((V0/V)**   (2.0/3.0)-1)**3)*B0_prime  + ((V0/V)**(2.0/3.0)-1)**2  *  (6.0-4.0*(V0/V)**(2.0/3.0))  ))
   return E
with open('datafile.dat') as   volume_pressure_energy: # open the file
volume_pressure_energy.next() # skip the first line
for line in volume_pressure_energy:
   volume = [float(n) for n in line.split()] # split the lines 
   # (removes linebreaks, tabs and spaces)
   # convert all items to floats.
   pressure = P(volume) # call my function
   energy = E(volume)   # call my function
   volume_pressure_energy.write('{}t{}t{}n'.format(volume, pressure, energy))
volume_pressure_energy.close()

如果每行只有一个数字,那么要阅读您应该做

volume = float(line)

line.split()返回一个列表,其中包含该行上的字符串的单个元素。所以当你做

volume = [float(n) for n in line.split()]

您说的是要包含Line的每个元素的列表。Split()转换为float。

这就是错误所说的。是一个列表。而且您不能将数字除以列表。如果同一行上可能有多个数字,并且您想要第一个。然后,您应该将音量的0元素传递给计算压力等的功能。

您要分割每一行并传递列表理解(列表),而不是数字,float,您应该传递到方法。

我认为您想做的是:

volume = [float(n) for n in line.split()][0]

可以进一步简化,以免将拆分的所有元素视为浮子到 float(line.split()[0])

错误告诉您您不能将整数除以列表。我怀疑您会使列表和Numpy阵列感到困惑。也许您可以使用asarray()之类的东西将列表转换为数组。

>>> import numpy as np
>>> l = [2.0, 3.0, 4.0]
>>> 1/l
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'list'
>>> a = np.asarray(l)
>>> 1/a
array([ 0.5       ,  0.33333333,  0.25      ])

一个更通用的解决方案是使用numpy.fromfile。它处理二进制文件并将其直接读取到ndarray。

volume_pressure_energy = np.fromfile('datafile.dat',dtype=float)

请参阅文档以获取更多信息。

最新更新