Python [在line.split()中为n的float(n)]在仅包含一列

时间:2016-06-19 21:27:46

标签: python file split line

我创建了一个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=" volume\tpressure\tenergy")

 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,即插入函数中的卷。 / p>

为什么会这样? 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=" volume\tpressure\tenergy")

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()

4 个答案:

答案 0 :(得分:1)

你正在拆分每一行并传递列表推导(列表)而不是你应该传递给方法的数字float。

我认为你想要做的是:

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

可以进一步简化为不将拆分的所有元素视为浮点数float(line.split()[0])

答案 1 :(得分:1)

错误告诉您不能按列表划分整数。我怀疑你是混淆列表和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      ])

答案 2 :(得分:1)

如果每行只有一个号码,那么你应该这样做

volume = float(line)

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

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

你说你想要一个包含line.split()转换为float的每个元素的列表。

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

答案 3 :(得分:1)

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

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

有关详情,请参阅文档。