import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import itertools
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import scipy.interpolate
import numpy as np, pylab
import os
from numpy import split
with open('zzz.txt') as fp:
for line in fp:
line = (line.split('=')[1].strip().split(','))
line = list(map(float, line))
print (line)
tck = interpolate.bisplrep(xx, yy, zz, s=0)
def givemeTL(X2,Y2):
return interpolate.bisplev(X2,Y2,tck)
文件(zzz.txt)包括:
xx = 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55
yy = 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09
zz = 0.75, 0.81, 0.83, 1.00, 0.92, 0.91, 0.745, 0.805, 0.82
我一直在收到错误
ValueError:无法将字符串转换为float:'xx = 0.15'
我每次都遇到这个问题,调用值是运行代码的一部分。但每次我改变调用方法时它都会给我错误。 我该如何解决? 最诚挚的问候
答案 0 :(得分:0)
data = {}
with open('zzz.txt') as fp:
for line in fp:
var, values = list(map(str.strip, line.split('=')))
values = list(map(float, values.split(',')))
data[var] = values
答案 1 :(得分:0)
我不会创建变量xx
但我会用data['xx']
创建字典 - 这是首选方法。
data = {} # dict for data from file
with open('zzz.txt') as fp:
for line in fp:
variable, numbers = line.split('=')
variable = variable.strip()
numbers = numbers.split(',')
numbers = list(map(float, numbers))
data[variable] = numbers
之后,您可以xx
作为data['xx']
print(data['xx'])
或者您可以分配给变量
xx = data['xx']
您可以使用data[variable] = numbers
而不是globals()[variable] = numbers
,而不是xx
而是变量xx = data['xx']
,但人们说它不是首选方法。