使用 csv 文件而不是静态值进行初始化
init with csv file instead of static values
我对 python 很陌生...我想将读取输入参数的静态方法更改为更动态的方法,改为从 .csv 文件读取输入参数。我将它从 _old 更改为 _new 如下...但它没有成功!如果有人能帮助我就好了。
physics_old.py
class Box(object):
def __init__(self, depth=5, width=10, height=3, density=7):
# Dimensions
self.depth = depth
self.width = width
self.height = height
self.density = density
sim_old.py
# Initialise instance, empty brackets for default see physics_old.py
Cuboid = Object(depth=6, width=12, height=2, density=7.5)
我想将这种静态方法更改为更加动态的方法,从 .csv 文件读取输入参数。我将其更改为以下...
physics_new.py
class Box(object):
def __init__(self, box_data):
labels = ['depth', 'width', 'height', 'density']
# Import csv file
data = pd.read_csv(box_data, skiprows=1, sep=';', index_col=False, names=labels)
# Dimensions
self.depth = data['depth']
self.width = data['width']
self.height = data['height']
self.density = data['density']
sim_new.py
# Initialise instance, empty brackets for default see physics_new.py
Cuboid = Object(data)
尝试将您的代码更改为:
class Box(object):
def __init__(self, box_data):
labels = ['depth', 'width', 'height', 'density']
# Import csv file
data = pd.read_csv(box_data, skiprows=1, sep=';', index_col=False, names=labels)
for _,row in data.iterrows():
# Dimensions
self.depth = row['depth']
self.width = row['width']
self.height = row['height']
self.density = row['density']
But you will be left with last row value in self.*variables*
. change your code flow
我对 python 很陌生...我想将读取输入参数的静态方法更改为更动态的方法,改为从 .csv 文件读取输入参数。我将它从 _old 更改为 _new 如下...但它没有成功!如果有人能帮助我就好了。
physics_old.py
class Box(object):
def __init__(self, depth=5, width=10, height=3, density=7):
# Dimensions
self.depth = depth
self.width = width
self.height = height
self.density = density
sim_old.py
# Initialise instance, empty brackets for default see physics_old.py
Cuboid = Object(depth=6, width=12, height=2, density=7.5)
我想将这种静态方法更改为更加动态的方法,从 .csv 文件读取输入参数。我将其更改为以下...
physics_new.py
class Box(object):
def __init__(self, box_data):
labels = ['depth', 'width', 'height', 'density']
# Import csv file
data = pd.read_csv(box_data, skiprows=1, sep=';', index_col=False, names=labels)
# Dimensions
self.depth = data['depth']
self.width = data['width']
self.height = data['height']
self.density = data['density']
sim_new.py
# Initialise instance, empty brackets for default see physics_new.py
Cuboid = Object(data)
尝试将您的代码更改为:
class Box(object):
def __init__(self, box_data):
labels = ['depth', 'width', 'height', 'density']
# Import csv file
data = pd.read_csv(box_data, skiprows=1, sep=';', index_col=False, names=labels)
for _,row in data.iterrows():
# Dimensions
self.depth = row['depth']
self.width = row['width']
self.height = row['height']
self.density = row['density']
But you will be left with last row value in
self.*variables*
. change your code flow