Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 指定值时出现键错误_Python_Excel_Python 2.7_Csv - Fatal编程技术网

Python 指定值时出现键错误

Python 指定值时出现键错误,python,excel,python-2.7,csv,Python,Excel,Python 2.7,Csv,我正试图将.csv数据输入python 这就是我的代码的样子: import csv revenue = {} prices = [] for i in range(1,21): prices.append(i) prices = tuple(prices) #convert to tuple to make immutable and faster with open("test.csv") as file_handle: file_reader = csv.rea

我正试图将.csv数据输入python

这就是我的代码的样子:

import csv

revenue = {}

prices = []
for i in range(1,21):
    prices.append(i)
prices = tuple(prices) #convert to tuple to make immutable and faster   

with open("test.csv") as file_handle:
    file_reader = csv.reader(file_handle)
    file_handle.readline()
    file_handle.readline()  #skip first 2 lines due to column header titles
    for row in file_reader:
        revenue[prices] = row[1] #assign revenue at each price-point

print revenue

print revenue[10]
这就是.csv数据的外观,也就是我的输入

0.01    1397371
0.02    1350610
0.03    1306431
0.04    1136959
0.05    1106950
0.06    1064727
0.07    1037497
0.08    1030768
0.09    976552
0.1     963091
0.11    949641
0.12    917551
0.13    884734
0.14    878675
0.15    775261
0.16    765643
0.17    756057
0.18    733458
0.19    723077
0.2     654178
第一列是价格,第二列是收入。因为我对价格的选择总是相同的,所以我实际上忽略了数据,只是创建了一个整数形式的价格列表,并将其转换为元组(因为我了解到,如果数据是不可变的,元组将处理得更快)

问题:如果我打印收入[10],我希望看到963091。相反,我得到了KeyError:20

当我打印收入时,我希望打印所有价格和相关收入,相反,我会打印整个价目表,然后是列表中最后一个价格的最终收入值(0.2),654178

我是python新手,所以对于新手提出的问题,我深表歉意,我一直在阅读并试图弄明白这一点,但我仍在努力——欢迎对我的方法提出任何建议,我可以利用我能得到的所有帮助


提前谢谢

revenue[prices]=row[1]
没有插入row的值(这是一个单项目列表),而是使用元组本身

>>> revenue[prices] = ''
>>> revenue
{(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20): ''}


要将行转换为价格和收入,请取第一项(唯一项),将其拆分为价格和收入,并将十进制价格转换为整数。

这是因为收入被声明为字典。无论何时访问字典中的元素,都应该使用键而不是数字。 因此,在这种情况下,收入[价格[10]]。注意元组从0到n开始。 因此,价格[10]实际上将是第11个要素

您还传递了收入[价格]=行[1]
将整行传递给revenue price。

KeyError当您试图通过不存在的键获取某个值时抛出。在构建收入dict-In-for循环时,您可以通过相同的键(即价格元组)分配每个新收入

revenue[prices] = row[1]
然后尝试通过不存在的键获取值。 要解决这个问题,您需要将收入分配给for循环中prices元组的相应元素

i = 0
for row in file_reader:
    revenue[prices[i]] = row[1] #assign revenue at each price-point
    i += 1
但要确保元组的长度不小于文件中的记录数

此外,还有一种较短的方法来构造价格元组:

prices = tuple(range(1,21))

revenue
是一个字典,它不包含值为10的键。根据您要查找的内容,您需要将收入定义为一个列表:
revenue=[]
,然后使用
revenue.append(第[1]行)
当您使用
10
时,
Keyerror
怎么可能是
20
?添加使用整个
prices
元组作为键的错误的实际回溯。这只是你字典中的一个键。也许你应该看看相关的问题。对此表示歉意,Keyerror是10,这是一个打字错误。谢谢你的回复Celeo,你有没有关于如何将值与每个键关联的建议,在这种情况下是价格?谢谢你的帮助!我认为,因为它在输入中循环,所以它会将每个收入金额按顺序关联到关联的价格(因为键的数量与值的数量相同),因为在for循环中使用收入[price]=行[1],所以您将把行[1]中的任何元素分配给具有键价格的dictionary元素。如果我错了,也请纠正我,但csv文件中的值不应该用逗号分隔。
prices = tuple(range(1,21))