如何在Python中将列表项用作变量名

时间:2019-07-18 21:55:27

标签: python loops syntax

我正在Python中解析一些数据,其中并非每个条目都具有每个属性,然后将其导出到csv。到目前为止,我认为最好的方法是遍历属性,在属性存在时分配属性值,在不存在时分配“ None”。但是我找不到正确的语法。

这是我尝试过的:

with open(file, 'w', encoding='utf8',newline='') as f:
    writer = csv.writer(f,delimiter=',')
    attribute_list=['height','weight', 'age']

    for attribute in attribute_list:
        try:
            attribute=person.attribute
        except Exception:
            attribute=None

    writer.writerow([height, weight,age])

我显然没有使用正确的语法,也无法弄清楚正确的语法是什么。

0 个答案:

没有答案
相关问题