0

I ran this program on Linux with Python 2.6.2 and it ran fine returning me with decimal values but when I run it on Python 2.7.2 on Windows it does not work and just gives a blank space for a while and then a memory error but I can't figure out why..I need it to run on Windows its a program to calculate stock equity (ROE). Thanks.

The CSV file needed to run the program is here. .

import csv

csvname = raw_input("Enter csv name: ")

sbuxfile = csv.reader(open(csvname), delimiter=',', quotechar='|')

# List of Data
row5, row8, row3, row7, avgequity, roe1, roe2 = ([] for i in range(7))

count = 0
# Grab data and numerical values from CSV.
for row in sbuxfile:
  count += 1
  if count == 8:     
     row8 = row
  elif count == 5:   
     row5 = row 
  elif count == 3:   
     row3 = row 
  elif count == 7:   
     row7 = row


a = 1

# Perform calculations for average equity and ROE.
while a < 8 :
   if a == 1:
     avgequity.append(round(float(row8[a]),2))
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / float(row8[a]))) 
   else:    
     avgequity.append(round(float((row8[a]),2) + float(row8[a-1]))/2)
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / ((float(row8[a]) + float(row8[a-1]))/2)))     
     a+=1 

print "\nAverage equity is " + str(avgequity) + "\n"
print "ROE method 1 is " + str(roe1) + "\n"
print "ROE method 2 is " + str(roe2)
5
  • What is your input file? Commented Oct 24, 2011 at 19:53
  • 1
    What happens if you change open(csvname) to open(csvname, 'rb')? Commented Oct 24, 2011 at 19:58
  • I just added the input file above you can download and test it out. Also when you add rb it still does not work just same blank page and after a while the memory error. Commented Oct 24, 2011 at 20:12
  • 1
    +1 @Steven: Or use "rU" since CSV essentially is text? Commented Oct 24, 2011 at 20:18
  • The rU doesn't work either, here is what the error is: File "H:\CSE231\proj05.py", line 34, in <module> roe1.append(float(row5[a]) / float(row8[a])) MemoryError Commented Oct 25, 2011 at 0:21

3 Answers 3

1

You have stuffed up the indentation of your a+=1 line, possibly because of (mis)use of tabs in your source file. As displayed here on SO, a will never be incremented and so the loop will never be exited.

Sign up to request clarification or add additional context in comments.

Comments

0

I added a few modifications to your script. It works fine on Python 2.7 for Windows. Here's the code:

import csv

csvname = raw_input("Enter csv name: ")

sbuxfile = csv.reader(open(csvname), delimiter=',', quotechar='|')
# List of Data
row5, row8, row3, row7, avgequity, roe1, roe2 = ([] for i in range(7))

count = 0
# Grab data and numerical values from CSV.
for row in sbuxfile:
  count += 1
  if count == 8:     
     row8 = row
  elif count == 5:   
     row5 = row 
  elif count == 3:   
     row3 = row 
  elif count == 7:   
     row7 = row


a = 1

# Perform calculations for average equity and ROE.
while a < 8 :
   if a == 1:
     avgequity.append(round(float(row8[a]),2))
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / float(row8[a])))
     a+=1   #added this line
   else:    
     avgequity.append(round(float(row8[a]),2) + float(row8[a-1])/2) #rewrote this line as it had an error
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / ((float(row8[a]) + float(row8[a-1]))/2)))     
     a+=1 

print "\nAverage equity is " + str(avgequity) + "\n"
print "ROE method 1 is " + str(roe1) + "\n"
print "ROE method 2 is " + str(roe2)

The output was : Average equity is [2071.11, 3505.7650000000003, 3325.3650000000002, 3273.6400000000003, 3398.375, 4187.76, 5197.549999999999]

ROE method 1 is [0.12812453225565035, 0.15742791098732495, 0.23651124740462906, 0.2532005689900426, 0.2944854035689894, 0.1283120464917753, 0.2573271287452037]

ROE method 2 is [0.12812453225565038, 0.17126298080734237, 0.21680660107401206, 0.2613058810726202, 0.29811440335236883, 0.1466466034500227, 0.2814118207249569]

1 Comment

Ahh I see, so the a+=1 loop exits now and the error in the other line is corrected just a parenthesis thing I see, thanks!
0

It helps to know where it's blowing up. You should try the Python debug module if the error isn't enough information. When executing Python e.g. "python ./script.py", try "python -m pdb ./script.py" and step through to see how far it gets. (type help for more info) If you're getting no feedback before the memory error.

How long is the csv file? (we'll assume you're using the same data on both platforms) There are 2 loops that might matter. When looping through sbuxfile (e.g. "for row in sbuxfile:"), you don't have a stop, but the 2nd loop (e.g. "while a < 8:") you only process 7 rows. Usually you get a memory error in cases were repeated data or large data is iterated over and maintained. If the csv is giant and you only wanted to read the first 7 lines, then break out of the csv read loop.

Also, I don't know what the purpose of equating the row values in your csv read loop is accomplishing. If you wanted to stuff the csv fields into array values, you should do so. e.g. row8 = row

I won't bother to ask if the ram memory size is different between the platforms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.