0

我怎样才能做到这一点:

reader = csv.reader(open(file_path, 'rb').read().splitlines(), delimiter=";")
p = Product()

for key, row in enumerate(reader):
    f = request.POST.get('select_%s' % key) // ex. productname
    p.f = row[key] // HOW TO?, p.f should be "productname" from the variable

希望你能帮帮我!

4

1 回答 1

2

用于setattr在运行时根据名称设置对象的属性:

reader = csv.reader(open(file_path, 'rb').read().splitlines(), delimiter=";")
p = Product()

for key, row in enumerate(reader):
    f = request.POST.get('select_%s' % key) // ex. productname
    // p.f should be "productname" from the variable
    setattr(p, f, row[key]) 
于 2013-01-31T08:34:31.563 回答