2 Replies - 344 Views - Last Post: 14 October 2011 - 01:00 AM Rate Topic: -----

#1 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Extracting values from lists and converting to ints

Posted 13 October 2011 - 08:08 PM

Hello,
so this is homework project in which we have to open a file from excel and take the values the numbers in each line and do an operation to find a final value, however the values are all in one cell. I am struggling with just extracting each number after I append them to their respective lists. I was hoping for some guidance, thanks (code is below).
,2003,2004,2005,2006,2007,2008,2009
Income statement,,,,,,,
Total Revenue,4075.52,5294.25,6369.3,7786.94,9411.5,9774.6,10707.4
Operating Income,420.85,606.49,780.52,893.95,1053.94,562,1419.4
Net Income,265.36,388.88,494.37,564.26,672.64,390.8,945.6
Balance Sheet,,,,,,,
Total Assets,2778.53,3386.54,3513.69,4428.94,5343.88,5576.8,6385.9
Total Equity,2071.11,2470.21,2090.26,2228.51,2284.12,3045.7,3674.7
Total Liabilities,707.42,916.33,1423.43,2200.43,3059.76,2531.1,2711.2
Cash Flow,,,,,,,
Operating,616.12,862.92,922.91,1131.63,1331.22,1389,1704.9
Investing,-616.42,-753.89,-220.62,-841.04,-1201.95,-421.1,-789.5
Financing,30.76,-66.55,-673.83,-155.33,-171.89,-642.2,-346
Net Cash Change,33.73,45.59,28.76,138.8,-31.34,330,564.2

import matplotlib as plt
import string
import numpy


listfile = []
yearlist = []
totalrev = []
opincome = []
netincome = []
totalassets = []
totalequity = []
totallib = []
operating = []
investing = []
financing = []
netchange = []

filename = raw_input('Enter the name of the file:')

try:
    fd = open(filename, "r")
except IOError:
    print 'Bad file name, try again'
   # filename = raw_input('Enter the name of the file:')
for line in fd:
    line_str = line.strip()
    line_str = line_str.split(',')
    listfile.append(line_str)

yearlist.append(listfile[0])
totalrev.append(listfile[2])
opincome.append(listfile[3])
netincome.append(listfile[4])
totalassets.append(listfile[6])
totalequity.append(listfile[7])
totallib.append(listfile[8])
operating.append(listfile[10])
investing.append(listfile[11])
financing.append(listfile[12])
netchange.append(listfile[13])

#ROE = netincome/totalequity
#ROE = (netincome/sales)*(sales/totalassets)*(totalassests/averageequity)














Is This A Good Question/Topic? 0
  • +

Replies To: Extracting values from lists and converting to ints

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Extracting values from lists and converting to ints

Posted 13 October 2011 - 08:41 PM

Lets assume we want to grab out the first value in the "Operating" line under Cash Flow. Lets say this line is loaded into listfile as index 10. We can then get access to this value by using two indexes. One to access the line, the second to access the value in that line...

print "First operating value: " + listfile[10][1]



So we are saying "Go to the 11 line, grab the second value (since "Operating" is the first value). Array indexes start from 0.

That is how you can pull out each value. To sum them up, think about looping through a given list. :)
Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Extracting values from lists and converting to ints

Posted 14 October 2011 - 01:00 AM

I'd just like to extend what Martyr2 said:

You are getting the lines from the file as strings so

>>> line = "Total Revenue,4075.52,5294.25,6369.3,7786.94,9411.5,9774.6,10707.4"
>>> splitLine = line.split(",")
>>> splitLine
['Total Revenue', '4075.52', '5294.25', '6369.3', '7786.94', '9411.5', '9774.6', '10707.4']


Will split the line into strings, which will give an error if you try to sum them up. So you either need to calculate them on the fly, as floats converted from strings, or calculate them all all store them back in the array.

From my previous example:

splitLine = ['Total Revenue', '4075.52', '5294.25', '6369.3', '7786.94', '9411.5', '9774.6', '10707.4']
# From 1 so it doesn't cast the first string to a float
>>> for i in range(1, len(splitLine)):
...    splitLine[i] = float(splitLine[i])
>>> splitLine
['Total Revenue', 4075.52, 5294.25, 6369.3, 7786.94, 9411.5, 9774.6, 10707.4]



They are now floating point numbers, so you can sum them or whatever you need to do!

This post has been edited by Simown: 14 October 2011 - 01:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1