Python Forum
Issue with Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with Dictionaries
#1
Hey All, For my assignment I have to read a csv file of stock information (without using outside modules) into a dictionary and then set up a program that allows the user to input a ticker symbol and output the market cap. My problem is that my code below generates a dictionary for each individual stock bc they were originally listed on different rows. Any help?



f_in_CRSP = open('/Users/Dino/PycharmProjects/Homework2/CRSP_StockFile_201612.csv')
header = next(f_in_CRSP)
for line in f_in_CRSP:
parts = line.strip('\n').split(',')
tkr = parts[4]
prc = parts[7]
if prc != "":
prc = abs(float(parts[7]))
else:
prc = 0
shrout = parts[8]
if shrout == "":
shrout = 0
else:
shrout = abs(float(parts[8]))
marketcap = (prc * shrout * 1000)
key = tkr
value = marketcap
my_dict = {key: value}
print(my_dict)
Reply
#2
You need to repost your code so we can see the indentation. See the BBCode link in my signature below for instructions. Also, please give us a few (like five) lines of the csv file you are supposed to be reading in.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020