I am attempting to feed .csv data into python.
This is what my code looks like:
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]
This is what the .csv data looks like, or my input.
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
First column is prices, and second column is revenue. Because my selection of prices are always the same, I actually ignored the data, and simply created a prices list in integer-form, which I converted to a tuple (since I read that if the data is immutable, tuples will process more quickly).
PROBLEM: if I print revenue[10] I want to see 963091. Instead I get KeyError: 20.
And when I print revenue, I expected all prices and associated revenues to be printed, instead, I get the entire price list printed, followed by the final revenue value for the last price in the list (0.2), 654178.
I'm new to python so I apologize for rookie questions, I've been reading and trying to figure this out and I'm still struggling - any advice on my approach is also welcomed, I can use all the help I can get.
Thanks in advance!