2016-02-11 81 views
0

我目前正在使用一个脚本来执行大量的CSV复制和粘贴操作。 (我们正在做一个产品导入文件的Magento)使用变量时遇到问题

def select_cell(m): 
    return 'row.'+ m 

def mageimport(f,x): #let x = table_size let y = color or wood_stain 
    sku_copy = [row.sku for index, row in f.iterrows() if type(select_cell(x)) != float] 
    option1 = [x for index, row in f.iterrows() if type(select_cell(x)) != float] 
    option2 = [select_cell(x) for index, row in f.iterrows() if type(select_cell(x)) != float] 

    df = {} 
    df = {'_super_products_sku':sku_copy, '_super_attribute_code':option1, '_super_attribute_option':option2} 
    return df 

如果我究竟使用代码如上所述,我把关于x的任何值返回一个字符串,而不是从我的数据帧的小区。我知道问题来自select_cell()函数,但我不知道如何绕过它。

如果我用row.table_size替换select_cell(),则信息出来正确。我可以做到这一点,但我宁愿让这个功能起作用。

任何意见表示赞赏。

+0

是'x'整数==表的长度,还是字符串'“table_size”',或其他? –

+0

@HughBothwell f是我所命名的数据框,x应该是列名中的任何一个。所以,x.table_size给了我具有产品大小的列。我想将它应用于其他专栏:f.colors,f.wood_stain等等。 – whyth3

回答

0

您正在返回一个字符串select_cell。它应该总是返回str作为type()

尝试使用(我不知道是哪一行,但我认为它是一个模块/类或全局可用的对象)。我要在这里创建一个简单的。

class row: 
    a = 10 
    b = 20 

def select_cell(m): 
    if hasattr(row, m): 
     return getattr(row, m) 
    return None 

assert select_cell('a') == 10 
assert select_cell('b') == 20 
assert select_cell('c') is None 
+0

'行'的各种想法。用于从数据框中选择特定的列,我命名为f。如果我输入'row.table_size'或'row.color'而不是使用select_cell(),它给了我期望在我的option2列表中的值。现在它给了我[row.table_size,row.table_size,row.table_size,row.table_size] – whyth3