【发布时间】:2020-03-06 11:33:56
【问题描述】:
我对 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)
【问题讨论】:
-
data['depth']这些将是深度 col 的所有行值,但是您之前只使用了一个值 -
嗨,您是否尝试打印“数据”变量?它是否正确返回?我也怀疑以这种方式创建对象。您正在将列表分配给一个值。
标签: python csv dataframe object init