Python,从文件中调用列值并插入值?

时间:2018-04-02 21:26:43

标签: python

Iteration, $X, $Y
1,0.581261625,0.010561718
2,0.470764982,0.01282931
3,0.44202262,0.011346878
4,0.447475587,0.011324991

我有这个表格中的这个文件,500个值我把前五个。对于同一次迭代中的每个$ X和$ Y,有一个Z将根据旧X和Y的以下代码计算。

我的以下代码是调用该文件,并为代码中已提供的每个$ X和$ Y找到等效的Z.

import sys
from math import log
import datetime
import re
import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import itertools
from scipy.optimize import curve_fit


##interpolation data for Z 
xx = 0.15, 0.33, 0.35, 0.5, 0.67, 0.8
yy = 0.01, 0.05, 0.08, 0.1, 0.15, 0.2
zz = 0.75, 0.81, 0.83, 1.00, 0.92, 0.91, 

# Read parameter file
parametersAndValues = {}
parameterFile = open('ParameterValues') #open(os.environ['RAVEN_PARAMETERFILE'])

try:
    for line in parameterFile:
        parametersAndValues[line.split()[0]] = ((float(line.split()[1])))
finally:
    parameterFile.close()

X = parametersAndValues.get('$X')
Y = parametersAndValues.get('$Y')


for parameter in parametersAndValues:
    ( parameter + ' = ' + str(parametersAndValues.get(parameter))) 

这部分代码是插入X和Y的值以找到Z

##interpolation data for TLMIXPAR
tck = interpolate.bisplrep(xx, yy, zz, s=0)    
def givemeomega(X,Y):
        result = interpolate.bisplev(X,Y,tck)
        return '{:.5f}'.format(min(1.0, result))

我一直得到的错误代码是ValueError: could not convert string to float: '$X,',因此我无法正确调用该值,也无法为$ X和$ Y找到合适的Z

感谢您提供的所有帮助

1 个答案:

答案 0 :(得分:1)

您在阅读文件时忘记了标题

这就是我读文件的方式

$GetAllUsernamesSQLStatement="SELECT Username,Name FROM users WHERE Manager=0";
$GetAllUsernamesQuery=mysql_query($GetAllUsernamesSQLStatement);
$NumberOfUsers=0;
while ($row = mysql_fetch_assoc($GetAllUsernamesQuery)){
    $Username[$NumberOfUsers]=$row['Username'];
    $Name[$NumberOfUsers]=$row['Name'];
    $NumberOfUsers++;
}
echo $NumberOfUsers;
$GetAllSkillsSQLStatement="SELECT SkillName FROM skills";
$GetAllSkillsQuery=mysql_query($GetAllSkillsSQLStatement);
$NumberOfSkills=0;
while ($row = mysql_fetch_assoc($GetAllSkillsQuery)){
    $Skill[$NumberOfSkills]=$row['SkillName'];
    $NumberofSkills++;
}
echo $NumberOfSkills;

_

X, Y = [], []
with open('ParameterValues', 'r') as parameterFile:
    header = parameterFile.readline()
    for line in parameterFile:
        items = [float(x) for x in line.split(',')]
        X.append(items[1])
        Y.append(items[2])

然后做你的插值,我真的不明白应该如何工作
(这是一个完全独立的问题)

print X
# [0.581261625, 0.470764982, 0.44202262, 0.447475587]

print Y
# [0.010561718, 0.01282931, 0.011346878, 0.011324991]