首页 > 解决方案 > 当我的密钥存在于数据集中的每一行时,为什么会出现密钥错误?

问题描述

尽管有很多与我类似的问题,但我觉得我的问题对我的数据集非常具体,这就是我发布这个问题的原因。我不明白为什么当我的数据集中每一行都存在我的密钥时,我不断收到密钥错误。

数据集(users2.csv):

ID,WordPress_Username,Hits,Adding a product on to the cart (Goal 4 Completions),Create an account (Goal 2 Completions),User Sign in (Goal 3 Completions)
GA1.4.301660640.1555323773,customerA,1627,6,0,0
GA1.4.1847365850.1555363120,flakeyflake,498,1,0,0
GA1.4.274974053.1555363505,miamia,498,1,0,0
GA1.4.871579651.1555364343,leopards,498,1,0,0
GA1.4.1481998750.1555363956,funkyfonk,497,1,0,0
GA1.4.151359083.1555984453,customer19,497,1,0,0
GA1.4.1572011796.1545148593,customer17,497,1,0,0
GA1.4.346994636.1555358930,customerB,496,1,0,0
GA1.4.1471680890.1555361868,customerCeeloo,494,1,0,0
GA1.4.638901714.1555362326,customerD,494,1,0,0
GA1.4.639010625.1555983888,customer18,492,1,0,0
GA1.4.301660640.1555323773,user,458,1,0,0
GA1.4.1176071976.1555361548,userman,391,1,0,0
GA1.4.1627788545.1551372413,user,313,0,0,1
GA1.4.1987994043.1555362748,customerE,302,0,0,0
GA1.4.346994636.1555358930,customer-a,288,1,0,0
GA1.4.241625205.1551270454,snowflake,207,2,0,0
GA1.4.715911968.1555364806,rolly,84,1,0,0
GA1.4.15322322.1555988035,Customer24,60,1,0,0
GA1.4.309775811.1555984825,SmoothCriminal,55,1,0,0
GA1.4.1572011796.1545148593,user,43,0,0,0
GA1.4.301660640.1555323773,customer1,39,0,0,0
GA1.4.287544727.1551561208,mrFreeze,24,1,1,0
GA1.4.1555142849.1551547974,akanshyagurung,22,1,1,0
GA1.4.274617959.1551470209,Futurehatesbbws,21,1,0,0
GA1.4.1259552180.1551547807,James,18,1,1,0
GA1.4.478253941.1551463155,Jaja,13,1,1,0
GA1.4.246541536.1551548196,kh0042,11,1,1,0
GA1.4.1694348881.1549839496,Superman,10,0,0,0
GA1.4.2092186657.1551470301,Lisa,9,1,1,0

我的python代码:

import csv
processed = []
columns = ['id', 'username','Adding a product on to the cart (Goal 4 Conversion Rate)','Create an account (Goal 2 Completions)', 
'User Sign in (Goal 3 Completions)', 'Hits']
i = 0

# Completed the main dimentions of the GA data
# getting details per unique user
with open('users2.csv') as users_data:
    user_dict = csv.DictReader(users_data)
    users = list(user_dict)

    for user in users:
        print(user)
        processed.append({
            'id': user['ID'],
            'username': user['WordPress_Username'],
            'Hits': user['Hits'],
            'Adding a product on to the cart (Goal 4 Conversion Rate)': user['Adding a product on to the cart (Goal 4 Conversion Rate)'],
            'Create an account (Goal 2 Completions)': user['Create an account (Goal 2 Completions)'],
            'User Sign in (Goal 3 Completions)': user['User Sign in (Goal 3 Completions)'],

        })

由于“将产品添加到购物车(目标 4 转化率)”,我不断收到关键错误

标签: pythonpython-3.xcsv

解决方案


您的密钥是您在代码中提到的,'Adding a product on to the cart (Goal 4 Completions)'而不是'Adding a product on to the cart (Goal 4 Conversion Rate)'您在processed.append代码中提到的。

columns在and in 中使用正确的键名,append代码应该可以工作

Adding a product on to the cart (Goal 4 Completions)': user['Adding a product on to the cart (Goal 4 Completions)'],

我现在得到的输出是。

OrderedDict([('ID', 'GA1.4.301660640.1555323773'), ('WordPress_Username', 'customerA'), ('Hits', '1627'), ('Adding a product on to the cart (Goal 4 Completions)', '6'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1847365850.1555363120'), ('WordPress_Username', 'flakeyflake'), ('Hits', '498'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.274974053.1555363505'), ('WordPress_Username', 'miamia'), ('Hits', '498'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.871579651.1555364343'), ('WordPress_Username', 'leopards'), ('Hits', '498'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1481998750.1555363956'), ('WordPress_Username', 'funkyfonk'), ('Hits', '497'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.151359083.1555984453'), ('WordPress_Username', 'customer19'), ('Hits', '497'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1572011796.1545148593'), ('WordPress_Username', 'customer17'), ('Hits', '497'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.346994636.1555358930'), ('WordPress_Username', 'customerB'), ('Hits', '496'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1471680890.1555361868'), ('WordPress_Username', 'customerCeeloo'), ('Hits', '494'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.638901714.1555362326'), ('WordPress_Username', 'customerD'), ('Hits', '494'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.639010625.1555983888'), ('WordPress_Username', 'customer18'), ('Hits', '492'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.301660640.1555323773'), ('WordPress_Username', 'user'), ('Hits', '458'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1176071976.1555361548'), ('WordPress_Username', 'userman'), ('Hits', '391'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1627788545.1551372413'), ('WordPress_Username', 'user'), ('Hits', '313'), ('Adding a product on to the cart (Goal 4 Completions)', '0'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '1')])
OrderedDict([('ID', 'GA1.4.1987994043.1555362748'), ('WordPress_Username', 'customerE'), ('Hits', '302'), ('Adding a product on to the cart (Goal 4 Completions)', '0'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.346994636.1555358930'), ('WordPress_Username', 'customer-a'), ('Hits', '288'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.241625205.1551270454'), ('WordPress_Username', 'snowflake'), ('Hits', '207'), ('Adding a product on to the cart (Goal 4 Completions)', '2'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.715911968.1555364806'), ('WordPress_Username', 'rolly'), ('Hits', '84'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.15322322.1555988035'), ('WordPress_Username', 'Customer24'), ('Hits', '60'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.309775811.1555984825'), ('WordPress_Username', 'SmoothCriminal'), ('Hits', '55'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1572011796.1545148593'), ('WordPress_Username', 'user'), ('Hits', '43'), ('Adding a product on to the cart (Goal 4 Completions)', '0'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.301660640.1555323773'), ('WordPress_Username', 'customer1'), ('Hits', '39'), ('Adding a product on to the cart (Goal 4 Completions)', '0'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.287544727.1551561208'), ('WordPress_Username', 'mrFreeze'), ('Hits', '24'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1555142849.1551547974'), ('WordPress_Username', 'akanshyagurung'), ('Hits', '22'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.274617959.1551470209'), ('WordPress_Username', 'Futurehatesbbws'), ('Hits', '21'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1259552180.1551547807'), ('WordPress_Username', 'James'), ('Hits', '18'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.478253941.1551463155'), ('WordPress_Username', 'Jaja'), ('Hits', '13'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.246541536.1551548196'), ('WordPress_Username', 'kh0042'), ('Hits', '11'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.1694348881.1549839496'), ('WordPress_Username', 'Superman'), ('Hits', '10'), ('Adding a product on to the cart (Goal 4 Completions)', '0'), ('Create an account (Goal 2 Completions)', '0'), ('User Sign in (Goal 3 Completions)', '0')])
OrderedDict([('ID', 'GA1.4.2092186657.1551470301'), ('WordPress_Username', 'Lisa'), ('Hits', '9'), ('Adding a product on to the cart (Goal 4 Completions)', '1'), ('Create an account (Goal 2 Completions)', '1'), ('User Sign in (Goal 3 Completions)', '0')])

推荐阅读