0
def get_all_cust_info():
    """function displays data as a list of dict of base  data"""
    reader = csv.DictReader(open("base data.csv", "rb"))
    all_rows = list()
    for row in reader:
    all_rows.append(row)
    return all_rows

get_all_cust_info() 的第一行输出。

   [{'totcust': '2', 'delfee': '1308', 'bskt_bnd': '0', 'distribution ': '>1', 'totords': '199', 'netsales': '1851'}, .......] 

我想创建一个删除键('delfee'和'netsales')并添加新键'order value'的新函数。这就是我所做的

    def cust_state():
        s = get_all_cust_info()
        for d in s:
            if d.has_key('delfee'):
               del d['delfee']
               print s  

但我收到了这个错误。

  AttributeError: 'list' object has no attribute 'has_key'

非常感谢您对此的帮助。

4

1 回答 1

1
if s.has_key('delfee'):

应该:

if d.has_key('delfee'):


def cust_state():
        s = get_all_cust_info()
        for d in s:
            if 'delfree' in d:
                del d['delfree']
            if 'netsales' in d:
                del d['netsales']
            d['ordervalue'] = something
        return s
于 2013-07-15T11:36:05.950 回答